Slide 9
Slide 9 text
{
"_id" : ObjectId("…06"),
"title": "this is my title",
"author": "bob",
"posted": …,
"pageViews": 5,
"tags": [
"fun",
"good",
"fun" ],
"comments": …
"other": …
},
{
"_id" : ObjectId("…07"),
"title": "this is your title",
"author": "dave",
"posted": …,
"pageViews": 7,
"tags": [
"fun",
"nasty" ],
…
},
{
"_id" : ObjectId("…08"),
"title": "this is some other title",
"author": "jane",
"posted": …,
"pageViews": 6,
"tags": [
"nasty",
"filthy" ],
…
}
{
"_id" : ObjectId("…06"),
"author": "bob",
"tags": [
"fun",
"good",
"fun" ],
"pageViews": 5
},
{
"_id" : ObjectId("…07"),
"author": "dave",
"tags": [
"fun",
"nasty" ],
"pageViews": 7
},
{
"_id" : ObjectId("…08"),
"author": "jane",
"tags": [
"nasty",
"filthy" ],
"pageViews": 6
}
{
"result" : [
{
"_id" : "filthy",
"docsByTag" : 1,
"viewsByTag" : 6,
"mostViewsByTag" : 6,
"avgByTag" : 6
},
{
"_id" : "good",
"docsByTag" : 1,
"viewsByTag" : 5,
"mostViewsByTag" : 5,
"avgByTag" : 5
},
{
"_id" : "nasty",
"docsByTag" : 2,
"viewsByTag" : 13,
"mostViewsByTag" : 7,
"avgByTag" : 6.5
},
{
"_id" : "fun",
"docsByTag" : 3,
"viewsByTag" : 17,
"mostViewsByTag" : 7,
"avgByTag" : 5.666…7
}], "Ok" : 1}
{
"_id" : ObjectId("…06"),
"author" : "bob",
"pageViews" : 5,
"tags" : "fun" },
{
"_id" : ObjectId("…06"),
"author" : "bob",
"pageViews" : 5,
"tags" : "good" },
{
"_id" : ObjectId("…06"),
"author" : "bob",
"pageViews" : 5,
"tags" : "fun" },
{
"_id" : ObjectId("…07"),
"author" : "dave",
"pageViews" : 7,
"tags" : "fun" },
{
"_id" : ObjectId("…07"),
"author" : "dave",
"pageViews" : 7,
"tags" : "nasty" },
{
"_id" : ObjectId("…08"),
"author" : "jane",
"pageViews" : 6,
"tags" : "nasty" },
{
"_id" : ObjectId("…08"),
"author" : "jane",
"pageViews" : 6,
"tags" : "filthy" }
Collection Intermediate-1 Intermediate-2 Result
$unwind : "$tags"
$project : {
author : 1,
tags : 1,
pageViews : 1}
$group : {
_id : "$tags",
docsByTag : { $sum : 1 },
viewsByTag : { $sum : "$pageViews" },
mostViewsByTag : { $max : "$pageViews" },
avgByTag : { $avg : "$pageViews" }}
MongoDB 2.2 Aggregation Framework
(follow the "fun" tag)
Follow the flow of three documents in a MongoDB 2.2 Collection as they undergo
three stages of an aggreation pipeline to the Result. The path of the three embed-
ded array elements “fun” highlights the $group expression. It pivots on array
elements, while calculating cross-document values. For more information, including
the actual aggregation pipeline and dataset, see
http://blog.mongolab.com/2012/07/aggregation-example/
or http://bit.ly/22agg
Infographic © 2012 ObjectLabs Corp. “MongoDB” and “Mongo” are l of 10gen, Inc. and are used with permission. Code via Chris Westin, 10gen. Tnx!