English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MongoDB mapReduce command

According to the MongoDB document, Map reduce is a data processing paradigm used to compress large amounts of data into useful aggregate results. MongoDB uses the mapReduce command for map reduce operations. MapReduce is commonly used to process large datasets.

MapReduce command

The following is the syntax for the basic 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
   }
)

map-The reduce function first queries the collection, then maps the results to emit keys-value pairs, and then reduces them based on keys with multiple values.

In the above syntax-

  • map is a JavaScript function that maps a key to a value and emits a key-value pairs

  • reduce is a JavaScript function used to reduce or group all documents with the same key

  • out Specify map-The location of the reduce query results

  • query Specify the optional selection criteria for selecting documents

  • sort Specify the optional sorting conditions

  • limit Specify the optional maximum number of documents to return

Using MapReduce

Consider the following document structure for storing user posts. The document stores the user's user_name and posting status.

{
   "post_text": "w"3codebox is an awesome website for tutorials
   "user_name": "mark"
   "status": "active"
}

Now, we will be usingpostsUse the mapReduce function on the collection to select all active posts, group them by user_name, and then count the number of posts for each user with 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 query outputs the following results-

{
   "result":
   "timeMillis": 9,
   "counts":
      "input": 4,
      "emit": 4,
      "reduce": 2,
      "output": 2
   ,}
   "ok": 1,}

The results show that there are a total of4documents that match the query (status: "active"), the map function emits4A document with key-value pairs, the reduce function then divides the mapped documents with the same key into2个。}

To view the results of this mapReduce query, please use the find 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 query returned the following results, indicating that both users tom and mark have two posts in active status–

{"_id" : "tom", "value" :} 2 }
{"_id" : "mark", "value" :} 2 }

In a similar way, MapReduce queries can be used to construct large and complex aggregation queries. The use of custom JavaScript functions leverages MapReduce, which is very flexible and powerful.