Mongodb的mapreduce

來源:互聯網
上載者:User

標籤:檢索   span   map   ...   option   fail   manual   err   定義   

簡單的看了一下mapreduce,我嘗試不看詳細的api去做一個group效果,結果遇到了很多問題,羅列在這裡,如果別人也遇到了類似的bug,可以檢索到結果。

  1. //先看person表的資料
  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" : "my second child", "age" : "i do not know" }
  6. { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南陽市", "building" : "桐柏縣" } }
  7. //使用彙總來做一個group by
  8. > db.person.aggregate({$group : {_id: ‘$age‘, count : {$sum : 1}}})
  9. { "_id" : "i do not know", "count" : 1 }
  10. { "_id" : 2, "count" : 1 }
  11. { "_id" : 30, "count" : 2 }
  12. //下面嘗試用map reduce來做同樣的group by效果
  13. //很簡單的邏輯 定義map函數 和reduce函數
  14.  
  15. > var m = function(){ emit(this.age,1) };
  16. > var r = function(key,values){
  17. ... var sum = 0;
  18. ... values.forEach(function(val){
  19. ... sum += val;
  20. ... });
  21. ... return sum;
  22. ... }
  23.  
  24. //然後在person上執行mapreduce 這樣會報錯 需要一個optionsOrOutString
  25. > db.person.mapReduce( m, r ).find();
  26. assert failed : need to supply an optionsOrOutString
  27. Error: assert failed : need to supply an optionsOrOutString
  28.     at Error (<anonymous>)
  29.     at doassert (src/mongo/shell/assert.js:11:14)
  30.     at assert (src/mongo/shell/assert.js:20:5)
  31.     at DBCollection.mapReduce (src/mongo/shell/collection.js:1343:5)
  32.     at (shell):1:11
  33. 2017-06-03T12:42:06.704+0800 E QUERY Error: assert failed : need to supply an optionsOrOutString
  34.     at Error (<anonymous>)
  35.     at doassert (src/mongo/shell/assert.js:11:14)
  36.     at assert (src/mongo/shell/assert.js:20:5)
  37.     at DBCollection.mapReduce (src/mongo/shell/collection.js:1343:5)
  38.     at (shell):1:11 at src/mongo/shell/assert.js:13
  39. //加了一個而空的option 又說要有一個string或者object的out參數
  40. > db.person.mapReduce( m, r,{} ).find();
  41. 2017-06-03T12:42:24.726+0800 E QUERY Error: map reduce failed:{
  42.    "errmsg" : "exception: ‘out‘ has to be a string or an object",
  43.    "code" : 13606,
  44.    "ok" : 0
  45. }
  46.     at Error (<anonymous>)
  47.     at DBCollection.mapReduce (src/mongo/shell/collection.js:1353:15)
  48.     at (shell):1:11 at src/mongo/shell/collection.js:1353
  49. //我嘗試定義一個變數 不行
  50. > var outstr;
  51. > db.person.mapReduce( m, r,{out:outstr} ).find();
  52. 2017-06-03T12:42:45.502+0800 E QUERY Error: map reduce failed:{
  53.    "errmsg" : "exception: ‘out‘ has to be a string or an object",
  54.    "code" : 13606,
  55.    "ok" : 0
  56. }
  57.     at Error (<anonymous>)
  58.     at DBCollection.mapReduce (src/mongo/shell/collection.js:1353:15)
  59.     at (shell):1:11 at src/mongo/shell/collection.js:1353
  60. //後來我瞭解到out需要的一個collection 於是我加了一個字串 ‘outt‘作為儲存資料的集合名字
  61.  
  62. > db.person.mapReduce( m, r,{out:‘outt‘} ).find();
  63. { "_id" : 2, "value" : 1 }
  64. { "_id" : 30, "value" : 2 }
  65. { "_id" : "i do not know", "value" : 1 }
  66. //此時outt中也儲存了資料 我不明白的是 不定義out參數 不是應該可以直接find就可以了嗎 為什麼要多此一舉呢
  67. > db.outt.find();
  68. { "_id" : 2, "value" : 1 }
  69. { "_id" : 30, "value" : 2 }
  70. { "_id" : "i do not know", "value" : 1 }

因為遇到了這麼多問題,所以看了Mongodb的文檔(https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/),梳理了一下,總結如下:

命令方式:

  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.              )

簡單方式:

  1. db.collection.mapReduce(map, reduce, {<out>, <query>, <sort>, <limit>, <finalize>, <scope>, <jsMode>, <verbose>})

 

Mongodb的mapreduce

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.