標籤:檢索 span map ... option fail manual err 定義
簡單的看了一下mapreduce,我嘗試不看詳細的api去做一個group效果,結果遇到了很多問題,羅列在這裡,如果別人也遇到了類似的bug,可以檢索到結果。
- //先看person表的資料
- > 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" : "my second child", "age" : "i do not know" }
- { "_id" : ObjectId("592ffd872108e8e79ea902b0"), "name" : "zjf", "age" : 30, "address" : { "province" : "河南省", "city" : "南陽市", "building" : "桐柏縣" } }
- //使用彙總來做一個group by
- > db.person.aggregate({$group : {_id: ‘$age‘, count : {$sum : 1}}})
- { "_id" : "i do not know", "count" : 1 }
- { "_id" : 2, "count" : 1 }
- { "_id" : 30, "count" : 2 }
- //下面嘗試用map reduce來做同樣的group by效果
- //很簡單的邏輯 定義map函數 和reduce函數
-
- > var m = function(){ emit(this.age,1) };
- > var r = function(key,values){
- ... var sum = 0;
- ... values.forEach(function(val){
- ... sum += val;
- ... });
- ... return sum;
- ... }
-
- //然後在person上執行mapreduce 這樣會報錯 需要一個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
- //加了一個而空的option 又說要有一個string或者object的out參數
- > db.person.mapReduce( m, r,{} ).find();
- 2017-06-03T12:42:24.726+0800 E QUERY Error: map reduce failed:{
- "errmsg" : "exception: ‘out‘ has to be 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
- //我嘗試定義一個變數 不行
- > 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‘ has to be 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
- //後來我瞭解到out需要的一個collection 於是我加了一個字串 ‘outt‘作為儲存資料的集合名字
-
- > db.person.mapReduce( m, r,{out:‘outt‘} ).find();
- { "_id" : 2, "value" : 1 }
- { "_id" : 30, "value" : 2 }
- { "_id" : "i do not know", "value" : 1 }
- //此時outt中也儲存了資料 我不明白的是 不定義out參數 不是應該可以直接find就可以了嗎 為什麼要多此一舉呢
- > db.outt.find();
- { "_id" : 2, "value" : 1 }
- { "_id" : 30, "value" : 2 }
- { "_id" : "i do not know", "value" : 1 }
因為遇到了這麼多問題,所以看了Mongodb的文檔(https://docs.mongodb.com/manual/reference/method/db.collection.mapReduce/),梳理了一下,總結如下:
命令方式:
- 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>
- }
- )
簡單方式:
- db.collection.mapReduce(map, reduce, {<out>, <query>, <sort>, <limit>, <finalize>, <scope>, <jsMode>, <verbose>})
Mongodb的mapreduce