MongoDB's MapReduce

Source: Internet
Author: User
Tags anonymous assert

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.

  1. First look at the data of the person table
  2. > Db.person. Find ();
  3. {"_id": ObjectId ("593011c8a92497992cdfac10"), "name": "xhj", "Age": 30 , "address": Dbref ("address", ObjectId ("59314b07e693aae7a5eb72ab"))}
  4. {"_id": ObjectId ("59301270a92497992cdfac11"), "name": "zzj", "Age ": 2}
  5. {"_id ": ObjectId (" 593015fda92497992cdfac12 ")," name ":" Span style= "color:darkred" >my second child , "age ": "i do not know "}
  6. {"_id": ObjectId ("592ffd872108e8e79ea902b0"), "name": "zjf", "Age": 30 , "address": {"Province": " Henan Province ", "City": " Nanyang ", "building" : " Tongbai County "}}
  7. Use aggregations to make a group by
  8. > db.person.aggregate ({$group: {_id: ' $age ', Count: {$sum: 1}}})
  9. {"_id": "Ido not know", "Count": 1}
  10. {"_id": 2, "Count": 1}
  11. {"_id": +, "Count": 2}
  12. Here's an attempt to use map reduce to do the same group by effect
  13. Very simple logic to define the map function and the reduce function
  14. > var m = function() {Emit (this. age,1)};
  15. > var r = function(key,values) {
  16. ... var sum = 0;
  17. ... Values.foreach (function(val) {
  18. ... sum + = val;
  19. ... });
  20. ... return sum;
  21. ... }
  22. And then execute the MapReduce on the person. This will cause an error to require a optionsoroutstring
  23. > Db.person.mapReduce (M, R). Find ();
  24. Assert Failed:need to supply an optionsoroutstring
  25. Error:assert Failed:need to supply an optionsoroutstring
  26. At Error (<anonymous>)
  27. At Doassert (src/mongo/shell/assert.js:11:14)
  28. At assert (Src/mongo/shell/assert.js:20:5)
  29. At Dbcollection.mapreduce (src/mongo/shell/collection.js:1343:5)
  30. at (Shell): 1:11
  31. 2017-06-03t12:42:06.704+0800 E QUERY Error:assert failed:need to supply an optionsoroutstring
  32. At Error (<anonymous>)
  33. At Doassert (src/mongo/shell/assert.js:11:14)
  34. At assert (Src/mongo/shell/assert.js:20:5)
  35. At Dbcollection.mapreduce (src/mongo/shell/collection.js:1343:5)
  36. at (Shell): 1:11 at Src/mongo/shell/assert.js:13
  37. Added an empty option to have a string or an out parameter of object
  38. > Db.person.mapReduce (M, r,{}). Find ();
  39. 2017-06-03t12:42:24.726+0800 E QUERY error:map reduce failed:{
  40. "errmsg": "Exception: ' Out ' have to is a string or an object",
  41. "code": 13606,
  42. "OK": 0
  43. }
  44. At Error (<anonymous>)
  45. At Dbcollection.mapreduce (src/mongo/shell/collection.js:1353:15)
  46. at (Shell): 1:11 at src/mongo/shell/collection.js:1353
  47. I'm trying to define a variable.
  48. > var outstr;
  49. > Db.person.mapReduce (M, r,{out:outstr}). Find ();
  50. 2017-06-03t12:42:45.502+0800 E QUERY error:map reduce failed:{
  51. "errmsg": "Exception: ' Out ' have to is a string or an object",
  52. "code": 13606,
  53. "OK": 0
  54. }
  55. At Error (<anonymous>)
  56. At Dbcollection.mapreduce (src/mongo/shell/collection.js:1353:15)
  57. at (Shell): 1:11 at src/mongo/shell/collection.js:1353
  58. Later I learned that a collection was needed so I added a string ' Outt ' as the collection name for the saved data
  59. > Db.person.mapReduce (M, r,{out: ' Outt '}). Find ();
  60. {"_id": 2, "value": 1}
  61. {"_id": +, "value": 2}
  62. {"_id": "Ido not know", "value": 1}
  63. 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?
  64. > Db.outt. Find ();
  65. {"_id": 2, "value": 1}
  66. {"_id": +, "value": 2}
  67. {"_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:

  1. Db.runcommand (
  2. {
  3. MapReduce: <collection>
  4. Map: <function;
  5. Reduce: <function;
  6. Finalize: <function;
  7. Out: <output>
  8. Query: <document;
  9. Sort: <document;
  10. Limit: <number>
  11. Scope: <document;
  12. Jsmode: <boolean;,
  13. Verbose: <boolean;,
  14. Bypassdocumentvalidation: <boolean;,
  15. Collation: <document>
  16. }
  17. )

Simple way:

    1. Db.collection.mapReduce (map, reduce, {<out>, <query>, <sort>, <limit>, <finalize>, < Scope>, <jsmode>, <verbose>})

MongoDB's MapReduce

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.