joined : ISODate("2011-03-02"), likes : "racquetball" } 

• The $group operator collects all documents the same value for the likes field and counts each grouping. With this information, $group creates a new document with two fields: 

– _id , which contains the likes value. 

– number , which is a generated field. The $sum operator increments this field by 1 for every document containing the given likes value. 

• The $sort operator sorts these documents by the number field in reverse order. 

• The $limit operator only includes the first 5 result documents. 

The results of aggregation would resemble the following: 

{ "_id" : "golf", "number" : 33 }, { "_id" : "racquetball", "number" : 31 }, { "_id" : "swimming", "number" : 24 }, { "_id" : "handball", "number" : 19 }, { "_id" : "tennis", "number" : 18 } 

## 7.3.3 Map-Reduce Examples 

In the mongo shell, the db.collection.mapReduce() method is a wrapper around the mapReduce command. The following examples use the db.collection.mapReduce() method: 

Consider the following map-reduce operations on a collection orders that contains documents of the following prototype: 

{ _id: ObjectId("50a8240b927d5d8b5891743c"), cust_id: "abc123", ord_date: new Date("Oct 04, 2012"), status: 'A', price: 25, items: [ { sku: "mmm", qty: 5, price: 2.5 }, { sku: "nnn", qty: 5, price: 2.5 } ] } 

