-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregation.txt
48 lines (48 loc) · 910 Bytes
/
aggregation.txt
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
# Example for finding user preferred ratings
db.ratings.aggregate([
{
$match: {
"_id.user_id": ObjectId("5e7a74ceea26cb1fcf0e5d71")
}
},
{
$lookup: {
from: "movies",
localField: "_id.movie_id",
foreignField: "_id",
as: "movies"
}
},
{
$project: {
"rating": 1,
"movies._id": 1,
"movies.directors": 1
}
},
{
$unwind: "$movies"
},
{
$unwind: "$movies.directors"
},
{
$group: {
_id: "$movies.directors.id",
name: {
$first: "$movies.directors.name"
},
rating: {
$avg : "$rating"
}
}
},
{
$sort: {
rating: -1
}
},
{
$limit: 3
}
])