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.

MapReduce Command

   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 -
  • map is a javascript characteristic that maps a fee with a key and emits a key-cost pair

  • reduce is a javascript feature that reduces or agencies all of the files having the identical key

  • 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 MapReduce

Consider 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",

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
   function()  emit(this.User_id,1); , 
   feature(key, values) go back Array.Sum(values),   

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
   function()  emit(this.User_id,1); , 
   function(key, values) go back Array.Sum(values),   
The above query gives the following end result which shows that both customers Peterson and Dewayne have two posts in energetic states
 "_id" : "Peterson", "value" : 2 
 "_id" : "Dewayne", "fee" : 2 
a comparable manner, MapReduce queries may be used to assemble big complex aggregation queries. The use of custom Javascript capabilities employ MapReduce which could be very bendy and powerful.

Welookups is optimized for learning.Copy right 2018 - 2019 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.