Skip to content

Instantly share code, notes, and snippets.

@aheckmann
Forked from TylerBrock/aggregation.js
Created August 16, 2012 14:48
Show Gist options
  • Save aheckmann/3370754 to your computer and use it in GitHub Desktop.
Save aheckmann/3370754 to your computer and use it in GitHub Desktop.
Mongo shell script and sample documents used for my aggregation talks 12/2011
// make sure we're using the right db; this is the same as "use aggdb;" in shell
db = db.getSiblingDB("aggdb");
// simple projection
var p1 = db.article.aggregate({
$project: { tags: 1, pageViews: 1 }
});
// unwinding an array
var u1 = db.article.aggregate({
$unwind: "$tags"
});
// combining pipeline operations
var p2 = db.article.aggregate(
{ $project: {
author: 1
, tags: 1
, pageViews: 1
}}
, { $unwind: "$tags" }
);
// pulling values out of subdocuments
var p3 = db.article.aggregate(
{ $project: {
otherfoo: "$other.foo"
, otherbar: "$other.bar"
}}
);
// projection includes a computed value
var p4 = db.article.aggregate(
{ $project: {
author: 1
, daveWroteIt: { $eq: ["$author", "dave"] }
}}
);
// projection includes a virtual (fabricated) document
var p5 = db.article.aggregate(
{ $project: {
author: 1
, pageViews: 1
, tags: 1
}
}
, { $unwind: "$tags" }
, { $project: {
author: 1
, subDocument: { foo: "$pageViews", bar: "$tags" }
}
}
);
// nested computed expression; $ifNull
var p7 = db.article.aggregate(
{ $project: {
theSum: {
$add: ["$pageViews", { $ifNull: ["$other.foo", "$other.bar"] }]
}
}
}
);
// dotted path inclusion; _id exclusion
var p8 = db.article.aggregate(
{ $project: {
_id: 0
, author: 1
, tags: 1
, "comments.author": 1
}
}
);
// simple sort
var p10 = db.article.aggregate({ $sort: { title: 1 }})
// dates
var p19 = db.article.aggregate(
{ $project: {
authors: 1
, seconds: {$second: "$posted"}
, minutes: {$minute: "$posted"}
, hour: {$hour: "$posted"}
, dayOfYear: {$dayOfYear: "$posted"}
, dayOfMonth: {$dayOfMonth: "$posted"}
, dayOfWeek: {$dayOfWeek: "$posted"}
, month: {$month: "$posted"}
, week: {$week: "$posted"}
, year: {$year: "$posted"}
, posted: 1
}
}
);
// ternary conditional ($cond) operator
var p21 = db.article.aggregate(
{ $project: {
_id: 0
, author: 1
, pageViews: {
$cond: [
{ $eq: ["$author", "dave"]} // if
, { $add:["$pageViews", 1000]}// then
, "$pageViews" // else
]
}
}
}
);
// simple matching
var m1 = db.article.aggregate({ $match: { author: "dave" }});
// combining matching with a projection
var m2 = db.article.aggregate(
{ $project: {
title: 1
, author: 1
, pageViews: 1
, tags: 1
, comments: 1
}
}
, { $unwind: "$tags" }
, { $match: { tags: "nasty" }}
);
// grouping
var g1 = db.article.aggregate(
{ $project: {
author: 1
, tags: 1
, pageViews: 1
}
}
, { $unwind: "$tags" }
, { $group: {
_id: "$tags"
, docsByTag: { $sum: 1 }
, viewsByTag: { $sum: "$pageViews" }
, mostViewsByTag: { $max: "$pageViews" }
, avgByTag: { $avg: "$pageViews" }
}
}
);
// $addToSet as an accumulator; can pivot data
var g5 = db.article.aggregate(
{ $project: {
author: 1
, tags: 1
}
}
, { $unwind: "$tags" }
, { $group: {
_id: "$tags"
, authors: { $addToSet: "$author" }
}
}
);
/* sample articles for aggregation demonstrations */
// make sure we're using the right db; this is the same as "use mydb;" in shell
db = db.getSiblingDB("aggdb");
db.article.drop();
db.article.save( {
title : "this is my title" ,
author : "bob" ,
posted : new Date(1079895594000) ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
});
db.article.save( {
title : "this is your title" ,
author : "dave" ,
posted : new Date(4121381470000) ,
pageViews : 7 ,
tags : [ "fun" , "nasty" ] ,
comments : [
{ author :"barbara" , text : "this is interesting" } ,
{ author :"jenny" , text : "i like to play pinball", votes: 10 }
],
other : { bar : 14 }
});
db.article.save( {
title : "this is some other title" ,
author : "jane" ,
posted : new Date(978239834000) ,
pageViews : 6 ,
tags : [ "nasty" , "filthy" ] ,
comments : [
{ author :"will" , text : "i don't like the color" } ,
{ author :"jenny" , text : "can i get that in green?" }
],
other : { bar : 14 }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment