-
Notifications
You must be signed in to change notification settings - Fork 0
/
ej9.mongodb
59 lines (55 loc) · 872 Bytes
/
ej9.mongodb
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
/**
* Ejercicio 9:
* Crear una vista con los 5 géneros con mayor cantidad de comentarios, junto con la
cantidad de comentarios.
*/
use('mflix');
pipeline_operation = [
{
$lookup: {
from: 'comments',
localField: '_id',
foreignField: 'movie_id',
as: 'comment_data'
}
},
{
$addFields: {
amount_comments: {
$size: '$comment_data'
}
}
},
{
$unwind: '$genres'
},
{
$group: {
_id: '$genres',
amount_comments: {
$sum: '$amount_comments'
}
}
},
{
$sort: {
amount_comments: -1
}
},
{
$limit: 5
},
{
$project: {
_id: 0,
genre: '$_id',
amount_comments: '$amount_comments'
}
}
];
db.createView(
'top5_commented_genres',
'movies',
pipeline_operation
);
db.top5_commented_genres.find({}).pretty();