MongoDB - Map Reduce
In this chapter we discuss MongoDB - Map Reduce
Map-lessen is a statistics processing paradigm for condensing huge volumes of records into beneficial aggregated consequences.
MongoDB uses mapReduce command for map-reduce operations. MapReduce is usually used for processing massive statistics units.
>db.Collection.MapReduce( characteristic() emit(key,cost);, //map feature characteristic(key,values) return reduceFunction, //lessen characteristic out: collection, query: record, type: file, restriction: wide variety )There are following syntax -
out specifies the vicinity of the map-reduce query result
query specifies the non-obligatory selection criteria for choosing documents
type specifies the elective kind standards
limit specifies the optionally available maximum quantity of documents to be back
Using MapReduceConsider the subsequent report structure storing person posts. The file shops user_name of the person and the repute of publish.
"post_text": "Angeline Jolie is splendid Hollywood actress ", "user_name": "angle", "fame":"active"we are able to use a mapReduce characteristic on our posts collection to pick all of the energetic posts, organization them on the basis of user_name and then be counted the number of posts through every consumer the use of the following code
>db.Posts.MapReduce( function() emit(this.User_id,1); , feature(key, values) go back Array.Sum(values), query:reputation:"active", out:"post_total" )The above mapReduce query outputs the subsequent result
"result" : "post_total", "timeMillis" : 9, "counts" : "enter" : four, "emit" : 4, "reduce" : 2, "output" : 2 , "ok" : 1,
The result suggests that a complete of 4 files matched the query (popularity:"energetic"), the map characteristic emitted 4 documents with key-value pairs and in the end the lessen characteristic grouped mapped documents having the same keys into 2.To see the result of this mapReduce query, use the discover operator
>db.Posts.MapReduce( function() emit(this.User_id,1); , function(key, values) go back Array.Sum(values), question:fame:"energetic", out:"post_total" ).Discover()The above query gives the following end result which shows that both customers Peterson and Dewayne have two posts in energetic states