Based on the MongoDB documentation, Map-reduce is a data processing paradigm for compressing huge volumes of data into useful collective results. MongoDB makes use of mapReduce command for map-reduce operations. Basically, MapReduce is used for processing large data sets.
Below is the syntax for the basic mapReduce command
Map-reduce function will first query the collection and will then map the result documents to produce key-value pairs. Then this is reduced based on the keys which have multiple values.
In the above syntax
Consider below document structure storing user posts. Document will store user_name of the user and the status of post.
Now, let’s use a mapReduce function in our posts collection for selecting all the active posts, grouping them on the basis of user_name and then counting the number of posts by each user using below code
Above mapReduce query results in below output
Result shows that a total of 4 documents matched the query (status:"active"), map function emitted 4 documents with key-value pairs and finally reduce function grouped mapped documents having the same keys into 2.
To see the result of this mapReduce query, use find operator as shown below
Above query will give below result which indicates that both users tom and mark have two posts in active states
MongoDB Related Interview Questions
|Python Interview Questions||C & Data Structures Interview Questions|
|Hadoop Interview Questions||Node.js Interview Questions|
|Scala Interview Questions||Socket Programming Interview Questions|
|HBase Interview Questions||Solaris Administrator Interview Questions|
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.