map

英[m?p] 美[m?p]

n. Map, celestial map; something similar to a map; face , face; genetic map (arrangement of genes on chromosomes)

vt. draw (a region, etc.) map; survey; detailed planning; [genetics] comparison

reduce

UK[r??dju:s] US[r??du:s]

vt. Reduce; reduce; reduce; make weak

vi. Reduce; diet ;Evaporate;(liquid)concentrate and thicken

MongoDB Map Reduce function syntax

Function:Map-Reduce is a computing model. Simply put, it decomposes a large batch of work (data) (MAP) for execution, and then merges the results into the final result (REDUCE). The Map-Reduce provided by MongoDB is very flexible and quite practical for large-scale data analysis.

Syntax: >db.collection.mapReduce(function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, //reduce function {out: collection, query: document, sort: document, limit: number }) Use MapReduce to implement two functions, the Map function and the Reduce function. The Map function calls emit(key, value) to traverse the collection. For all the records in the record, pass the key and value to the Reduce function for processing. The Map function must call emit(key, value) to return the key-value pair.

Parameters: map: mapping function (generates a sequence of key-value pairs as reduce function parameters). reduce statistical function, the task of the reduce function is to turn key-values ??into key-value, that is, to turn the values ??array into a single value value. . out The statistical results are stored in a collection (if not specified, a temporary collection will be used, which will be automatically deleted after the client is disconnected). query is a filtering condition. Only documents that meet the condition will call the map function. (query.limit, sort can be combined at will) The sort sorting parameters combined with sort and limit (which also sort the documents before sending them to the map function) can optimize the grouping mechanism limit and limit the upper limit of the number of documents sent to the map function (if there is no limit, Using sort alone is of little use)

MongoDB Map Reduce function example

>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "mark",
   "status":"active"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "mark",
   "status":"active"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "mark",
   "status":"active"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "mark",
   "status":"active"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "mark",
   "status":"disabled"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "php",
   "status":"disabled"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "php",
   "status":"disabled"
})
WriteResult({ "nInserted" : 1 })
>db.posts.insert({
   "post_text": "php中文網(wǎng),最全的技術(shù)文檔。",
   "user_name": "php",
   "status":"active"
})
WriteResult({ "nInserted" : 1 })
現(xiàn)在,我們將在 posts 集合中使用 mapReduce 函數(shù)來選取已發(fā)布的文章(status:"active"),并通過user_name分組,計算每個用戶的文章數(shù):

>db.posts.mapReduce( 
   function() { emit(this.user_name,1); }, 
   function(key, values) {return Array.sum(values)}, 
      {  
         query:{status:"active"},  
         out:"post_total" 
      }
)
以上 mapReduce 輸出結(jié)果為:

{
        "result" : "post_total",
        "timeMillis" : 23,
        "counts" : {
                "input" : 5,
                "emit" : 5,
                "reduce" : 1,
                "output" : 2
        },
        "ok" : 1
}
結(jié)果表明,共有4個符合查詢條件(status:"active")的文檔, 在map函數(shù)中生成了4個鍵值對文檔,最后使用reduce函數(shù)將相同的鍵值分為兩組。



具體參數(shù)說明:

result:儲存結(jié)果的collection的名字,這是個臨時集合,MapReduce的連接關(guān)閉后自動就被刪除了。

timeMillis:執(zhí)行花費的時間,毫秒為單位

input:滿足條件被發(fā)送到map函數(shù)的文檔個數(shù)

emit:在map函數(shù)中emit被調(diào)用的次數(shù),也就是所有集合中的數(shù)據(jù)總量

ouput:結(jié)果集合中的文檔個數(shù)(count對調(diào)試非常有幫助)

ok:是否成功,成功為1

err:如果失敗,這里可以有失敗原因,不過從經(jīng)驗上來看,原因比較模糊,作用不大

使用 find 操作符來查看 mapReduce 的查詢結(jié)果:

>db.posts.mapReduce( 
   function() { emit(this.user_name,1); }, 
   function(key, values) {return Array.sum(values)}, 
      {  
         query:{status:"active"},  
         out:"post_total" 
      }
).find()
以上查詢顯示如下結(jié)果,兩個用戶 tom 和 mark 有兩個發(fā)布的文章:

{ "_id" : "mark", "value" : 4 }
{ "_id" : "php", "value" : 1 }
用類似的方式,MapReduce可以被用來構(gòu)建大型復(fù)雜的聚合查詢。

Map函數(shù)和Reduce函數(shù)可以使用 JavaScript 來實現(xiàn),使得MapReduce的使用非常靈活和強(qiáng)大。