Skip to content

Instantly share code, notes, and snippets.

@panduroab
Last active September 1, 2017 19:53
Show Gist options
  • Select an option

  • Save panduroab/8940b2f9bc7c007196ccc41793b567c8 to your computer and use it in GitHub Desktop.

Select an option

Save panduroab/8940b2f9bc7c007196ccc41793b567c8 to your computer and use it in GitHub Desktop.
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)
});
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