mongo db 2.2 aggregation like a champ
TRANSCRIPT
Map/Reduce pain
Aggregation framework
- Concepts
- Code demo
...Whatever comes our way
Emit (0..* )
Reduce
Documents
Finalize
• One chance at transform
• Good for large IO
• In/Out of Javascript costly
• Imparts "non-interactiveness"
• All I wanted was a group-by!
{ "_id" : "silver", "score" : 17, "house" : "A" }
db.scores.aggregate({$group:
{_id:"$house", avg:{$avg:"$score"}}});
{
"result":[
{"_id" : "B","avg" : 20.333333333333332},
{"_id" : "A","avg" : 19.333333333333332}
],
"ok":1
}
Pipeline
Operators ("stage")
Collectors, Transformers (operator type)
Expressions ("transform“,”evaluate”)
operator
Documents
All docs*
One at a time*
"The" => "A"
* not really, not always, it depends, kinda.
operator operator
$group
$match
$limit
$skip
$project
$unwind
$sort $group is why we are here..
but need not be last stage
$group
In: doc, Out: aggregated by _id specified
$project
In: doc, Out: new doc, shaped to your liking
$unwind
In: array, Out: same doc + 1 array element
Download - mongodb.org v 2.2
Ask – 10gen, Google groups, IRC (people use
them)
Consult / Train - [email protected]