15.09.2017 Views

the-magical-marvels-of-mongodb-slides

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Using Accumulators<br />

Anything specified after <strong>the</strong> group key is considered an accumulator. Accumulators take a<br />

single expression and compute <strong>the</strong> expression for grouped documents.<br />

> db.potions.aggregate([<br />

{"$group": {"_id":<br />

"$vendor_id",<br />

"total": {"$sum": 1} }}<br />

])<br />

SHELL<br />

Inventory<br />

Group key<br />

Results<br />

Will add 1 for each<br />

matching document<br />

Total number <strong>of</strong> documents per vendor!<br />

Accumulator<br />

$group<br />

{"_id": "Kettlecooked", "total": 2},<br />

{"_id": "Brewers", "total": 1,},<br />

{"_id": "Leprechaun Inc”, "total": 1}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!