-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_mongo_aggregate.js
33 lines (29 loc) · 966 Bytes
/
node_mongo_aggregate.js
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
var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://127.0.0.1:27017/";
MongoClient.connect(url, { useUnifiedTopology: true }, function (err, db) {
if (err) throw err;
var dbo = db.db("netflix");
dbo.collection("ratings").aggregate([
{ $group: { _id: "$movieId", average: { $avg: "$rating" } } },
{
$lookup: {
from: "movies",
localField: "_id",
foreignField: "movieId",
as: "movie"
}
},
{ $project: { _id: 0, average: 1, "movie.movieTitle": 1}},
{ $limit: 4 }
]).toArray(function (err, res) {
if (err) throw err;
// console.log(JSON.stringify(res));
res.forEach(element => {
console.log(
'Title: ' + element.movie[0].movieTitle + '\n' +
'Average: ' + element.average + '\n'
);
});
db.close();
});
});