Last active
September 1, 2017 19:53
-
-
Save panduroab/8940b2f9bc7c007196ccc41793b567c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Model = require("./model"); | |
| let agg = model.aggregate(); | |
| //match the results | |
| agg.match({ | |
| rated: "R" | |
| }); | |
| //Make the projection | |
| agg.project({ | |
| _id: 1, //or true | |
| actors: 1, | |
| year: 1 | |
| }); | |
| //group | |
| agg.group({ | |
| _id: "$year", | |
| /* | |
| //Also you can do this | |
| _id: { | |
| year: '$year', | |
| rated: '$rated' | |
| } | |
| */ | |
| movieInfo:{ | |
| $push: "$$ROOT" | |
| }, | |
| total:{ | |
| $sum: 1 | |
| } | |
| }); | |
| //unwind to separate the collection in objects | |
| agg.unwind("movieInfo"); | |
| agg.exec((err, data)=>{ | |
| console.log(data) | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| agg.match({rated:"R"}); | |
| agg.project({ | |
| _id: 1, | |
| actors: 1, | |
| year: 1 | |
| }); | |
| agg.group({ | |
| _id: "$year", | |
| actors: { | |
| $push: "$actors" | |
| }, | |
| total: { | |
| $sum: 1 | |
| } | |
| }); | |
| agg.unwind('actors'); | |
| agg.unwind('actors'); | |
| agg.project({ | |
| _id: 1, | |
| actor: "$actors", | |
| }); | |
| agg.group({ | |
| _id: "$_id", | |
| actors: { | |
| $push: "$actor" | |
| }, | |
| total: { | |
| $sum: 1 | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment