Database Reference
In-Depth Information
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
Like SQL databases, MongoDB also offers facilities for grouping and
aggregating data in queries. The original facility for aggregation was either
the group() or mapReduce() commands, but versions of MongoDB after
2.2 also support an optimized aggregate() command.
Unlike SQL, the pipeline command uses a pipeline approach for computing
its results, taking an array of filtering and grouping commands used to
reach a final result. This is easiest to understand in action, so first build a
collection with some example data:
> abc =
['A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
> db.createCollection("aggtest");
> for(var i=0;i<1000;i++) {
... db.aggtest.insert({
... first:abc[Math.floor(Math.random()*abc.length)],
... second:abc[Math.floor(Math.random()*abc.length)],
... count:Math.floor(1000*Math.random())
... });
... }
> db.aggtest.find({})
{ "_id" : ObjectId("53213bc8ae5fcad63d0563e9"),
"first" : "S", "second" : "W", "count" : 762 }
Search WWH ::




Custom Search