mongo db 2.2 aggregation like a champ

10

Upload: nuri-halperin

Post on 18-Jul-2015

113 views

Category:

Self Improvement


2 download

TRANSCRIPT

Page 1: Mongo db 2.2 aggregation like a champ
Page 2: Mongo db 2.2 aggregation like a champ

Map/Reduce pain

Aggregation framework

- Concepts

- Code demo

...Whatever comes our way

Page 3: Mongo db 2.2 aggregation like a champ

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!

Page 4: Mongo db 2.2 aggregation like a champ

{ "_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

}

Page 5: Mongo db 2.2 aggregation like a champ

Pipeline

Operators ("stage")

Collectors, Transformers (operator type)

Expressions ("transform“,”evaluate”)

Page 6: Mongo db 2.2 aggregation like a champ

operator

Documents

All docs*

One at a time*

"The" => "A"

* not really, not always, it depends, kinda.

operator operator

Page 7: Mongo db 2.2 aggregation like a champ

$group

$match

$limit

$skip

$project

$unwind

$sort $group is why we are here..

but need not be last stage

Page 8: Mongo db 2.2 aggregation like a champ

$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

Page 9: Mongo db 2.2 aggregation like a champ
Page 10: Mongo db 2.2 aggregation like a champ

Download - mongodb.org v 2.2

Ask – 10gen, Google groups, IRC (people use

them)

Consult / Train - [email protected]