A simple look at MapReduce, I try not to look at the detailed API to do a group effect, the results encountered a lot of problems, listed here, if others have encountered a similar bug, you can retrieve the results.
- First look at the data of the person table
- > Db.person. Find ();
- {"_id": ObjectId ("593011c8a92497992cdfac10"), "name": "xhj", "Age": 30 , "address": Dbref ("address", ObjectId ("59314b07e693aae7a5eb72ab"))}
- {"_id": ObjectId ("59301270a92497992cdfac11"), "name": "zzj", "Age ": 2}
- {"_id ": ObjectId (" 593015fda92497992cdfac12 ")," name ":" Span style= "color:darkred" >my second child , "age ": "i do not know "}
- {"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "zjf", "Age": 30 , "address": {"Province": " Henan Province ", "City": " Nanyang ", "building" : " Tongbai County "}}
- Use aggregations to make a group by
- > db.person.aggregate ({$group: {_id: ' $age ', Count: {$sum: 1}}})
- {"_id": "Ido not know", "Count": 1}
- {"_id": 2, "Count": 1}
- {"_id": +, "Count": 2}
- Here's an attempt to use map reduce to do the same group by effect
- Very simple logic to define the map function and the reduce function
- > var m = function() {Emit (this. age,1)};
- > var r = function(key,values) {
- ... var sum = 0;
- ... Values.foreach (function(val) {
- ... sum + = val;
- ... });
- ... return sum;
- ... }
- And then execute the MapReduce on the person. This will cause an error to require a optionsoroutstring
- > Db.person.mapReduce (M, R). Find ();
- Assert Failed:need to supply an optionsoroutstring
- Error:assert Failed:need to supply an optionsoroutstring
- At Error (<anonymous>)
- At Doassert (src/mongo/shell/assert.js:11:14)
- At assert (Src/mongo/shell/assert.js:20:5)
- At Dbcollection.mapreduce (src/mongo/shell/collection.js:1343:5)
- at (Shell): 1:11
- 2017-06-03t12:42:06.704+0800 E QUERY Error:assert failed:need to supply an optionsoroutstring
- At Error (<anonymous>)
- At Doassert (src/mongo/shell/assert.js:11:14)
- At assert (Src/mongo/shell/assert.js:20:5)
- At Dbcollection.mapreduce (src/mongo/shell/collection.js:1343:5)
- at (Shell): 1:11 at Src/mongo/shell/assert.js:13
- Added an empty option to have a string or an out parameter of object
- > Db.person.mapReduce (M, r,{}). Find ();
- 2017-06-03t12:42:24.726+0800 E QUERY error:map reduce failed:{
- "errmsg": "Exception: ' Out ' have to is a string or an object",
- "code": 13606,
- "OK": 0
- }
- At Error (<anonymous>)
- At Dbcollection.mapreduce (src/mongo/shell/collection.js:1353:15)
- at (Shell): 1:11 at src/mongo/shell/collection.js:1353
- I'm trying to define a variable.
- > var outstr;
- > Db.person.mapReduce (M, r,{out:outstr}). Find ();
- 2017-06-03t12:42:45.502+0800 E QUERY error:map reduce failed:{
- "errmsg": "Exception: ' Out ' have to is a string or an object",
- "code": 13606,
- "OK": 0
- }
- At Error (<anonymous>)
- At Dbcollection.mapreduce (src/mongo/shell/collection.js:1353:15)
- at (Shell): 1:11 at src/mongo/shell/collection.js:1353
- Later I learned that a collection was needed so I added a string ' Outt ' as the collection name for the saved data
- > Db.person.mapReduce (M, r,{out: ' Outt '}). Find ();
- {"_id": 2, "value": 1}
- {"_id": +, "value": 2}
- {"_id": "Ido not know", "value": 1}
- At this point Outt also saved the data I do not understand is not to define the out parameter should not be able to directly find it? Why superfluous?
- > Db.outt. Find ();
- {"_id": 2, "value": 1}
- {"_id": +, "value": 2}
- {"_id": "Ido not know", "value": 1}
Because of encountering so many problems, so looked at the MongoDB document (https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/), combed a bit, Summarized as follows:
Command mode:
- Db.runcommand (
- {
- MapReduce: <collection>
- Map: <function;
- Reduce: <function;
- Finalize: <function;
- Out: <output>
- Query: <document;
- Sort: <document;
- Limit: <number>
- Scope: <document;
- Jsmode: <boolean;,
- Verbose: <boolean;,
- Bypassdocumentvalidation: <boolean;,
- Collation: <document>
- }
- )
Simple way:
- Db.collection.mapReduce (map, reduce, {<out>, <query>, <sort>, <limit>, <finalize>, < Scope>, <jsmode>, <verbose>})
MongoDB's MapReduce