As consistent with the MongoDB documentation, Map-reduce is a statistics processing paradigm for condensing large volumes of facts into useful aggregated outcomes. MongoDB makes use of mapReduce command for map-lessen operations. MapReduce is usually used for processing large data units.
MapReduce Command
Following is the syntax of the primary mapReduce command −
>db.collection.mapReduce(
function() {emit(key,value);}, //map function
function(key,values) {return reduceFunction}, { //reduce function
out: collection,
query: document,
sort: document,
limit: number
}
)
The map-lessen function first queries the collection, then maps the result documents to emit key-price pairs, that's then reduced based on the keys that have a couple of values.
In the above syntax −
- map is a javascript function that maps a fee with a key and emits a key-cost pair
- lessen is a javascript feature that reduces or agencies all the documents having the identical key
- out specifies the location of the map-reduce question end result
- query specifies the optional selection standards for choosing files
- sort specifies the optional type standards
- restriction specifies the optional maximum wide variety of documents to be returned
Using MapReduce
Consider the subsequent file shape storing user posts. The document stores user_name of the person and the reputation of submit.
{
"post_text": "tutorialspoint is an awesome website for tutorials",
"user_name": "mark",
"status":"active"
}
Now, we will use a mapReduce function on our posts collection to select all the lively posts, institution them on the basis of user_name and then be counted the wide variety of posts by way of every person the use of the following code −
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
)
The above mapReduce question outputs the subsequent result −
{
"result" : "post_total",
"timeMillis" : 9,
"counts" : {
"input" : 4,
"emit" : 4,
"reduce" : 2,
"output" : 2
},
"ok" : 1,
}
The result suggests that a total of four files matched the question (fame:"active"), the map feature emitted 4 documents with key-fee pairs and in the end the lessen feature grouped mapped files having the equal keys into 2.
To see the result of this mapReduce question, use the locate operator −
>db.posts.mapReduce(
function() { emit(this.user_id,1); },
function(key, values) {return Array.sum(values)}, {
query:{status:"active"},
out:"post_total"
}
).find()
The above question offers the following end result which indicates that each users tom and mark have posts in active states −
{ "_id" : "tom", "value" : 2 }
{ "_id" : "mark", "value" : 2 }
In a similar way, MapReduce queries can be used to assemble massive complex aggregation queries. The use of custom Javascript functions employ MapReduce which is very flexible and powerful.