-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase-queries.js
More file actions
512 lines (476 loc) · 11.2 KB
/
database-queries.js
File metadata and controls
512 lines (476 loc) · 11.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Common Database Queries for Humorous Podcast Application
// These are example queries you can use in MongoDB Compass or your application
use podcastDB;
// ===========================================
// 1. USER MANAGEMENT QUERIES
// ===========================================
// Get all users with their subscription details
db.users.aggregate([
{
$lookup: {
from: "userPrompts",
localField: "_id",
foreignField: "userId",
as: "prompts"
}
},
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "createdBy",
as: "episodes"
}
},
{
$project: {
username: 1,
email: 1,
"profile.displayName": 1,
"subscription.plan": 1,
"stats.totalPrompts": 1,
"stats.totalEpisodes": 1,
promptCount: { $size: "$prompts" },
episodeCount: { $size: "$episodes" },
lastActive: 1
}
}
]);
// Get users by humor style preference
db.users.find(
{ "profile.preferences.humorStyle": "sarcastic" },
{ username: 1, "profile.displayName": 1, "profile.preferences.humorStyle": 1 }
);
// Get premium users with their episode count
db.users.aggregate([
{ $match: { "subscription.plan": { $in: ["premium", "pro"] } } },
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "createdBy",
as: "episodes"
}
},
{
$project: {
username: 1,
"subscription.plan": 1,
episodeCount: { $size: "$episodes" },
lastActive: 1
}
},
{ $sort: { episodeCount: -1 } }
]);
// ===========================================
// 2. CONTENT CREATION QUERIES
// ===========================================
// Get all pending prompts ordered by priority
db.userPrompts.find(
{ "processing.status": "pending" },
{ prompt: 1, category: 1, mood: 1, "processing.priority": 1, createdAt: 1 }
).sort({ "processing.priority": -1, createdAt: 1 });
// Get prompts by category and mood
db.userPrompts.find(
{
category: "personal",
mood: { $in: ["sarcastic", "witty"] }
},
{ prompt: 1, category: 1, mood: 1, createdAt: 1 }
).sort({ createdAt: -1 });
// Get prompts that led to published episodes
db.userPrompts.aggregate([
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "sourcePrompts",
as: "episodes"
}
},
{
$match: {
"episodes.status": "published"
}
},
{
$project: {
prompt: 1,
category: 1,
mood: 1,
episodeCount: { $size: "$episodes" },
episodeTitles: "$episodes.title"
}
}
]);
// ===========================================
// 3. PODCAST EPISODE QUERIES
// ===========================================
// Get all published episodes with analytics
db.podcastEpisodes.aggregate([
{ $match: { status: "published" } },
{
$lookup: {
from: "users",
localField: "createdBy",
foreignField: "_id",
as: "creator"
}
},
{
$project: {
title: 1,
description: 1,
episodeNumber: 1,
duration: 1,
publishedAt: 1,
"analytics.downloads": 1,
"analytics.plays": 1,
"analytics.likes": 1,
creator: { $arrayElemAt: ["$creator.username", 0] },
tags: 1
}
},
{ $sort: { publishedAt: -1 } }
]);
// Get episodes by humor level
db.podcastEpisodes.find(
{
"script.segments.humorLevel": { $gte: 8 },
status: "published"
},
{ title: 1, "script.segments.humorLevel": 1, tags: 1 }
);
// Get episodes with highest engagement
db.podcastEpisodes.aggregate([
{ $match: { status: "published" } },
{
$addFields: {
totalEngagement: {
$add: [
"$analytics.downloads",
"$analytics.plays",
"$analytics.shares",
"$analytics.likes"
]
}
}
},
{
$project: {
title: 1,
"analytics.downloads": 1,
"analytics.plays": 1,
"analytics.shares": 1,
"analytics.likes": 1,
totalEngagement: 1,
publishedAt: 1
}
},
{ $sort: { totalEngagement: -1 } },
{ $limit: 10 }
]);
// Get episodes by tag
db.podcastEpisodes.find(
{
tags: { $in: ["coffee", "relationships"] },
status: "published"
},
{ title: 1, tags: 1, publishedAt: 1 }
).sort({ publishedAt: -1 });
// ===========================================
// 4. VOICE GENERATION QUERIES
// ===========================================
// Get all voice generation jobs by status
db.voiceGenerationJobs.aggregate([
{
$group: {
_id: "$status",
count: { $sum: 1 },
avgDuration: { $avg: "$processing.duration" }
}
},
{ $sort: { count: -1 } }
]);
// Get failed voice generation jobs
db.voiceGenerationJobs.find(
{ status: "failed" },
{
episodeId: 1,
"processing.errorMessage": 1,
"processing.retryCount": 1,
createdAt: 1
}
).sort({ createdAt: -1 });
// Get voice generation performance by provider
db.voiceGenerationJobs.aggregate([
{
$group: {
_id: "$voiceConfig.provider",
totalJobs: { $sum: 1 },
completedJobs: {
$sum: { $cond: [{ $eq: ["$status", "completed"] }, 1, 0] }
},
avgDuration: { $avg: "$processing.duration" },
successRate: {
$avg: { $cond: [{ $eq: ["$status", "completed"] }, 1, 0] }
}
}
},
{
$project: {
provider: "$_id",
totalJobs: 1,
completedJobs: 1,
avgDuration: { $round: ["$avgDuration", 2] },
successRate: { $round: [{ $multiply: ["$successRate", 100] }, 2] }
}
},
{ $sort: { successRate: -1 } }
]);
// ===========================================
// 5. RSS FEED QUERIES
// ===========================================
// Get all active RSS feeds with episode counts
db.rssFeeds.aggregate([
{ $match: { status: "active" } },
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "rssFeedId",
as: "episodes"
}
},
{
$project: {
name: 1,
url: 1,
"config.title": 1,
"stats.totalSubscribers": 1,
episodeCount: { $size: "$episodes" },
lastEpisodeDate: { $max: "$episodes.publishedAt" }
}
},
{ $sort: { "stats.totalSubscribers": -1 } }
]);
// Get RSS feeds that need updating
db.rssFeeds.find(
{
status: "active",
$or: [
{ "stats.lastUpdated": { $lt: new Date(Date.now() - 24 * 60 * 60 * 1000) } },
{ "stats.lastUpdated": { $exists: false } }
]
},
{ name: 1, url: 1, "stats.lastUpdated": 1 }
);
// ===========================================
// 6. ANALYTICS QUERIES
// ===========================================
// Get analytics summary for an episode
db.analytics.aggregate([
{ $match: { episodeId: ObjectId("EPISODE_ID_HERE") } },
{
$group: {
_id: "$type",
count: { $sum: 1 },
totalDuration: { $sum: "$event.duration" },
avgDuration: { $avg: "$event.duration" }
}
},
{ $sort: { count: -1 } }
]);
// Get user engagement over time
db.analytics.aggregate([
{
$group: {
_id: {
year: { $year: "$event.timestamp" },
month: { $month: "$event.timestamp" },
day: { $dayOfMonth: "$event.timestamp" }
},
totalEvents: { $sum: 1 },
uniqueUsers: { $addToSet: "$userId" }
}
},
{
$project: {
date: {
$dateFromParts: {
year: "$_id.year",
month: "$_id.month",
day: "$_id.day"
}
},
totalEvents: 1,
uniqueUserCount: { $size: "$uniqueUsers" }
}
},
{ $sort: { date: -1 } },
{ $limit: 30 }
]);
// Get top performing episodes by downloads
db.analytics.aggregate([
{ $match: { type: "download" } },
{
$group: {
_id: "$episodeId",
downloadCount: { $sum: 1 },
uniqueUsers: { $addToSet: "$userId" }
}
},
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "_id",
as: "episode"
}
},
{
$project: {
episodeTitle: { $arrayElemAt: ["$episode.title", 0] },
downloadCount: 1,
uniqueUserCount: { $size: "$uniqueUsers" }
}
},
{ $sort: { downloadCount: -1 } },
{ $limit: 10 }
]);
// Get analytics by source/platform
db.analytics.aggregate([
{
$group: {
_id: {
source: "$event.source",
platform: "$metadata.platform"
},
eventCount: { $sum: 1 },
uniqueUsers: { $addToSet: "$userId" }
}
},
{
$project: {
source: "$_id.source",
platform: "$_id.platform",
eventCount: 1,
uniqueUserCount: { $size: "$uniqueUsers" }
}
},
{ $sort: { eventCount: -1 } }
]);
// ===========================================
// 7. CONTENT TEMPLATE QUERIES
// ===========================================
// Get most used templates
db.contentTemplates.aggregate([
{ $match: { isActive: true } },
{
$project: {
name: 1,
category: 1,
"usage.timesUsed": 1,
"usage.rating": 1,
"settings.humorLevel": 1
}
},
{ $sort: { "usage.timesUsed": -1 } }
]);
// Get templates by category and humor level
db.contentTemplates.find(
{
category: "intro",
"settings.humorLevel": { $gte: 7 },
isActive: true
},
{ name: 1, description: 1, "settings.humorLevel": 1, "usage.rating": 1 }
);
// ===========================================
// 8. SYSTEM CONFIGURATION QUERIES
// ===========================================
// Get all configuration by category
db.systemConfig.find(
{ category: "voice" },
{ key: 1, value: 1, description: 1 }
);
// Get configuration for current environment
db.systemConfig.find(
{
$or: [
{ environment: "all" },
{ environment: "production" }
]
},
{ key: 1, value: 1, type: 1, category: 1 }
);
// ===========================================
// 9. COMPLEX ANALYTICS QUERIES
// ===========================================
// Get user journey from prompt to episode
db.userPrompts.aggregate([
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "sourcePrompts",
as: "episodes"
}
},
{
$lookup: {
from: "users",
localField: "userId",
foreignField: "_id",
as: "user"
}
},
{
$project: {
prompt: 1,
category: 1,
mood: 1,
episodeCount: { $size: "$episodes" },
episodeTitles: "$episodes.title",
username: { $arrayElemAt: ["$user.username", 0] },
createdAt: 1
}
},
{ $match: { episodeCount: { $gt: 0 } } },
{ $sort: { createdAt: -1 } }
]);
// Get content performance by humor style
db.users.aggregate([
{
$lookup: {
from: "podcastEpisodes",
localField: "_id",
foreignField: "createdBy",
as: "episodes"
}
},
{
$unwind: "$episodes"
},
{
$group: {
_id: "$profile.preferences.humorStyle",
avgDownloads: { $avg: "$episodes.analytics.downloads" },
avgPlays: { $avg: "$episodes.analytics.plays" },
avgLikes: { $avg: "$episodes.analytics.likes" },
episodeCount: { $sum: 1 }
}
},
{
$project: {
humorStyle: "$_id",
avgDownloads: { $round: ["$avgDownloads", 2] },
avgPlays: { $round: ["$avgPlays", 2] },
avgLikes: { $round: ["$avgLikes", 2] },
episodeCount: 1
}
},
{ $sort: { avgDownloads: -1 } }
]);
print("✅ All example queries are ready to use!");
print("💡 Replace 'EPISODE_ID_HERE' with actual ObjectId when testing specific queries");