Concept of Map Reduce in MongoDB

Last updated on May 30 2022
Satyen Sahu

Table of Contents

Concept of Map Reduce in MongoDB

MongoDB – Map Reduce

As per the MongoDB documentation, Map-reduce may be a data processing paradigm for condensing large volumes of knowledge into useful aggregated results. MongoDB uses mapReduce command for map-reduce operations. MapReduce is usually used for processing large data sets.

MapReduce Command

Subsequent is that the syntax of 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
}
)
The map-reduce function first queries the collection, then maps the result documents to emit key-value pairs, which is then reduced based on the keys that have multiple values.
In the above syntax −
• map is a javascript function that maps a value with a key and emits a key-value pair
• reduce is a javascript function that reduces or groups all the documents having the equivalent key
• out specifies the location of the map-reduce query result
• query specifies the optional selection criteria for choosing documents
• sort specifies the optional sort criteria
• limit specifies the optional maximum number of documents to be returned

Using MapReduce

Consider the subsequent document structure storing user posts. The document stores user_name of the user and therefore the status of post.
{
“post_text”: “tecklearn is an awesome website for tutorials”,
“user_name”: “mark”,
“status”:”active”
}
Now, we’ll use a mapReduce function on our posts collection to pick all the active posts, group them on the basis of user_name and then count the amount of posts by each user using the subsequent 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 subsequent result −
{
“result” : “post_total”,
“timeMillis” : 9,
“counts” : {
“input” : 4,
“emit” : 4,
“reduce” : 2,
“output” : 2
},
“ok” : 1,
}
The result shows that a total of 4 documents matched the query (status:”active”), the map function emitted 4 documents with key-value pairs and finally the reduce function grouped mapped documents having the equivalent keys into 2.
To see the result of this mapReduce query, 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 gives the subsequent result which indicates that both users tom and mark have two posts in active states −
{ “_id” : “tom”, “value” : 2 }
{ “_id” : “mark”, “value” : 2 }
In a similar manner, MapReduce queries can be used to construct large complex aggregation queries. The utilization of custom Javascript functions make use of MapReduce which is very flexible and powerful.
So, this brings us to the end of blog. This Tecklearn ‘Concept of Map Reduce in MongoDB’ helps you with commonly asked questions if you are looking out for a job in MongoDB and No-SQL Database Domain.
If you wish to learn and build a career in MongoDB or No-SQL Database domain, then check out our interactive, MongoDB Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

MongoDB Training

MongoDB Training

About the Course

Tecklearn’s MongoDB Training helps you to master the NoSQL database. The course makes you job-ready by letting you comprehend schema design, data modelling, replication, and query with MongoDB through real-time examples. Along with this, you’ll also gain hands-on expertise in installing, configuring, and maintaining the MongoDB environment, including monitoring and operational strategies from this online MongoDB training. Upon completion of this online training, you will hold a solid understanding and hands-on experience with MongoDB.

Why Should you take MongoDB Training?

• MongoDB – a $36 billion to a $40 billion market growing at 8% to 9% annually – Forbes.com
• Average salary of a Mongo DB certified professional is $134k – Indeed.com
• MongoDB has more than 900 customers, including 27 Fortune 100 companies like Cisco, eBay, eHarmony, MetLife & Salesforce.com

What you will Learn in this Course?

Introduction to MongoDB and Importance of NoSQL
• Understanding the basic concepts of RDBMS
• What is NoSQL Database and its significance?
• Challenges of RDBMS and How NoSQL suits Big Data needs
• Types of NoSQL Database and NoSQL vs. SQL Comparison
• CAP Theorem and Implementing NoSQL
• Introduction to MongoDB and its advantages
• Design Goals for MongoDB Server and Database, MongoDB tools
• Collection, Documents and Key Value Pair
• Introduction to JSON and BSON documents
• MongoDB installation
MongoDB Installation
• MongoDB Installation
• Basic MongoDB commands and operations,
• Mongo Chef (MongoGUI) Installation
Schema Design and Data Modelling
• Why Data Modelling?
• Data Modelling Approach
• Data Modelling Concepts
• Difference between MongoDB and RDBMS modelling
• Challenges for Data Modelling
• Model Relationships between Documents
• Data Model Examples and Patterns
• Model Tree Structures
CRUD Operations
• MongoDB Architecture
• CRUD Introduction and MongoDB CRUD Concepts
• MongoDB CRUD Concerns (Read and Write Operations)
• Cursor Query Optimizations and Query Behaviour in MongoDB
• Distributed Read and Write Queries
• MongoDB Datatypes
Indexing and Aggregation Framework
• Concepts of Data aggregation and types and data indexing concepts
• Introduction to Aggregation
• Approach to Aggregation
• Types of Aggregation: Pipeline, MapReduce and Single Purpose
• Performance Tuning
MongoDB Administration
• Administration concepts in MongoDB
• MongoDB Administration activities: Health check, recovery, backup, database sharing and profiling, performance tuning etc.
• Backup and Recovery Methods for MongoDB
• Export and Import of Data from MongoDB
• Run time configuration of MongoDB
MongoDB Security
• Security Introduction
• MongoDB security Concepts and security approach
• MongoDB integration with Java and Robomongo
Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Concept of Map Reduce in MongoDB"

Leave a Message

Your email address will not be published. Required fields are marked *