-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodb.js
More file actions
189 lines (173 loc) · 4.6 KB
/
mongodb.js
File metadata and controls
189 lines (173 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Agora NoSQL Component — MongoDB
// Collection: activity_logs
// Mirrors user activity that does not need relational structure:
// page views, upvote events, and search queries.
// Run these commands in the MongoDB shell or mongosh.
use agora_nosql;
// -----------------------------------------------------------------------
// Collection: activity_logs
// Each document records one user action with flexible metadata.
// -----------------------------------------------------------------------
// Insert — page view events
db.activity_logs.insertMany([
{
user_id: 1,
event: "page_view",
target: "course_feed",
course_id: 2,
timestamp: ISODate("2025-09-10T09:00:00Z"),
metadata: { device: "desktop", duration_sec: 120 }
},
{
user_id: 2,
event: "page_view",
target: "problem_detail",
problem_id: 1,
timestamp: ISODate("2025-09-11T09:05:00Z"),
metadata: { device: "mobile", duration_sec: 45 }
},
{
user_id: 3,
event: "page_view",
target: "course_feed",
course_id: 2,
timestamp: ISODate("2025-09-12T08:30:00Z"),
metadata: { device: "desktop", duration_sec: 200 }
},
{
user_id: 5,
event: "page_view",
target: "user_profile",
profile_user_id: 1,
timestamp: ISODate("2025-09-13T14:10:00Z"),
metadata: { device: "desktop", duration_sec: 30 }
},
{
user_id: 7,
event: "page_view",
target: "resource_list",
course_id: 2,
timestamp: ISODate("2025-09-14T10:00:00Z"),
metadata: { device: "tablet", duration_sec: 90 }
}
]);
// Insert — upvote events
db.activity_logs.insertMany([
{
user_id: 4,
event: "upvote",
target: "post",
target_id: 7,
timestamp: ISODate("2025-09-15T10:30:00Z"),
metadata: {}
},
{
user_id: 6,
event: "upvote",
target: "answer",
target_id: 1,
timestamp: ISODate("2025-09-11T11:30:00Z"),
metadata: {}
},
{
user_id: 9,
event: "upvote",
target: "problem",
target_id: 8,
timestamp: ISODate("2025-09-16T09:30:00Z"),
metadata: {}
},
{
user_id: 11,
event: "upvote",
target: "post",
target_id: 3,
timestamp: ISODate("2025-09-12T09:00:00Z"),
metadata: {}
},
{
user_id: 13,
event: "upvote",
target: "answer",
target_id: 12,
timestamp: ISODate("2025-09-16T11:00:00Z"),
metadata: {}
}
]);
// Insert — search events
db.activity_logs.insertMany([
{
user_id: 2,
event: "search",
query: "normalization 3NF",
results_count: 4,
timestamp: ISODate("2025-09-11T09:00:00Z"),
metadata: { course_filter: 2 }
},
{
user_id: 8,
event: "search",
query: "quicksort pivot",
results_count: 2,
timestamp: ISODate("2025-09-12T10:00:00Z"),
metadata: { course_filter: 1 }
},
{
user_id: 14,
event: "search",
query: "trig substitution",
results_count: 3,
timestamp: ISODate("2025-09-14T14:00:00Z"),
metadata: { course_filter: 5 }
},
{
user_id: 17,
event: "search",
query: "composite index mysql",
results_count: 5,
timestamp: ISODate("2025-09-25T10:00:00Z"),
metadata: { course_filter: 2 }
},
{
user_id: 20,
event: "search",
query: "improper integral diverge",
results_count: 2,
timestamp: ISODate("2025-09-26T09:00:00Z"),
metadata: { course_filter: 5 }
}
]);
// -----------------------------------------------------------------------
// Queries
// -----------------------------------------------------------------------
// Find all activity logs for user 2
db.activity_logs.find({ user_id: 2 });
// Find all upvote events
db.activity_logs.find({ event: "upvote" });
// Find all page_view events on course_feed pages
db.activity_logs.find({ event: "page_view", target: "course_feed" });
// Find all search events with more than 3 results
db.activity_logs.find({ event: "search", results_count: { $gt: 3 } });
// Find all events after September 15, 2025
db.activity_logs.find({
timestamp: { $gt: ISODate("2025-09-15T00:00:00Z") }
});
// Count events grouped by event type
db.activity_logs.aggregate([
{ $group: { _id: "$event", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
]);
// Most active users by number of logged events
db.activity_logs.aggregate([
{ $group: { _id: "$user_id", total_events: { $sum: 1 } } },
{ $sort: { total_events: -1 } },
{ $limit: 5 }
]);
// Top search queries
db.activity_logs.aggregate([
{ $match: { event: "search" } },
{ $group: { _id: "$query", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
]);
// Create an index on user_id and timestamp for fast per-user activity lookups
db.activity_logs.createIndex({ user_id: 1, timestamp: -1 });