MapReduce can be used in MongoDB for complex aggregate queries.
Map and Reduce functions can be implemented using JavaScript.
You can use db. runCommand or mapReduce command to execute a MapReduce operation:
- Db. runCommand (
- {Mapreduce:<Collection>,
- Map:<Mapfunction>,
- Reduce:<Performancefunction>
- [, Query:<QueryFilter object>]
- [, Sort:<SortThe query. useful for optimization>]
- [, Limit:<NumberOf objects to return from collection>]
- [, Out:<Output-collectionName>]
- [, Keeptemp:<True| False>]
- [, Finalize:<Finalizefunction>]
- [, Scope:<ObjectWhere fields go into javascript global scope>]
- [, Verbose: true]
- }
- );
- # Or use a packaged Helper command
- Db. collection. mapReduce (mapfunction, performancefunction [, options]);
If no output is defined, a temporary collection is generated by default after it is executed. when the client is disconnected, the collection is automatically cleared.
A simple column has a collection of user_addr. The result is as follows:
- Db. user_addr.find ({'uid': 'test @ sohu.com '})
- {"_ Id": ObjectId ("4bbde0bf600ac3c3cc7245e3"), "Uid": "yangsong@sohu.com", "Al ":[
- {
- "Nn": "test-1 ",
- "Em": "test-1@sohu.com ",
- },
- {
- "Nn": "test-2 ",
- "Em": "test-2@sohu.com ",
- },
- {
- "Nn": "test-3 ",
- "Em": "test-3@sohu.com ",
- }
- ]}
Stores the contact information (Al) corresponding to a user (Uid). to query the number of contacts corresponding to each Em, create the following MapReduce
- M=Function(){
- For (index in this. Al ){
- Emit (this. Al [index]. Em, 1 );
- }
- }
- R=Function(K, vals ){
- VarSum=0;
- For (index in vals ){
- Sum + = vals [index];
- >}
- Return sum;
- }
- Res=Db. User_addr.mapReduce (m, r)
- {
- "Result": "tmp. mr. mapreduce_1272267853_1 ",
- "TimeMillis": 29,
- "Counts ":{
- "Input": 5,
- "Emit": 26,
- "Output": 26
- },
- "OK": 1,
- }
- Db [res. result]. find ()
The group function in MongoDB also needs to be implemented using MapReduce.
For example, group by uid and calculate the number of contacts for each uid.
- R=Function(Obj, prev ){
- Prev. sum + = obj. Al. length;
- }
- Db. user_addr.group ({key: {'uid': 1}, reduce: r, initial: {sum: 0 }})