EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MongoDB Tutorial MongoDB MapReduce
 

MongoDB MapReduce

Priya Pedamkar
Article byPriya Pedamkar

Updated February 28, 2023

MongoDB MapReduce

 

 

Definition of MongoDB MapReduce

MongoDB MapReduce is a data processing technique used for large data and the useful aggregated result of large data in MongoDB. We need to use this command to process a large volume of collected data or MapReduce operations, MapReduce in MongoDB basically used for a large volume of data sets processing. This command is handy and important in MongoDB to process a large volume of data sets. This command in MongoDB is used to process a large volume of data, map function, and reduce function is the two primary inputs in MongoDB.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax and Parameter

Below is the syntax of the MapReduce command.

Syntax:

collection_name.mapReduce (function (Map function), function (Reduce function))

collection.mapReduce (
function () {emit(key(Key value of map function), value);},
function (key, values) {return reduceFunction (Reduce function for MapReduce command)}, {
out: <collection>,
query: <document>,
sort: <document>,
limit: <number>,
finalize: <function>,
scope: < document>,
jsMode: <boolean>,
verbose: <boolean>,
bypassDocumentValidation: <boolean>
})

Parameter:

Below is the parameter description:

  • Collection name: Collection name is defined as retrieved documents from the collection by using the MapReduce command. We can process large volumes of data using the MapReduce method in MongoDB.
  • Map Reduce: It is a data processing technique used for large data and the useful aggregated result of large data in MongoDB. MapReduce command is handy and important in MongoDB.
  • Options: Options is specified that additional parameter used with this MapReduce command.
  • Out: Out is specify that result location of the MapReduce operation in MongoDB. We can set output as a primary member and on the secondary members, we can only set an inline output.
  • Query: Query defines as the selection criteria of a document in MongoDB. Using the query, we have to define select criteria of MapReduce in MongoDB.
  • Sort: This is used to sort the documents from collections. This option is mainly useful for optimization using the MapReduce method in MongoDB.
  • Limit: Limit is a specified method that limits the no of documents for the input by using the MapReduce method.
  • Finalize: It is an optional parameter method in MongoDB. It will modify the output and follows the reduce method.
  • Scope: Scope is used to specify that global variables that were accessible from the map using the MapReduce method.
  • JsMode: It will specify whether the data will convert into BSON format at the time execution of functions.
  • Verbose: Default value of verbose in MapReduce command is false. It will specify the timing information.
  • Collation: Collationis optional parameter of the MapReduce method in MongoDB. It will specify that collation which is to be used for MapReduce operations.

How does MapReduce Command Works in MongoDB?

Below is the working of the MapReduce command in MongoDB.

  • It states that data processing techniques for a large volume of data.
  • It contains two functions of javascript are as follows.
    • Map: It is a javascript function that was used in a MapReduce Command of MongoDB. It will associate that maps a value with key and emits the pair value and key.
    • Reduce: It is a javascript function that was used in a MapReduce Command of MongoDB. It will reduce the single object from all the values which were associated with the key.
  • Reduce function and map function are the two primary inputs of the MapReduce command in MongoDB.
  • The scope is an important parameter, and it is used to specify that global variables that were accessible from the map using the MapReduce method.
  • We can use a limit method to restrict the number of counts of documents from the collection.
  • We have used collation with the MapReduce method in MongoDB.

Examples of MongoDB MapReduce

Below are the examples:

We have taken an example of the order_test table to describe how the MapReduce method in MongoDB is as follows. Below is the data description of order_test table are as follows.

db.order_test.find ()

MongoDB MapReduce-1.1

For defining examples, we need to follow the below steps are as follows.

  • Define the map function.
  • Define the reduce function.
  • Perform a MapReduce operation.
  • Verify the result of MapReduce command.

1. Define Map Function

  • We have defining map function to process each statement from the collection. The below example shows map function are as follows.
  • In the below example, we have to define a map function name as mapFun1.

var mapFun1 = function() {emit(this.customer_address, this.price_of_product);};

MongoDB MapReduce-1.2

2. Define Reduce Function

  • We have defined reduce function to reduce the single object from all MongoDB MapReduce method values. The below example shows reduce function as follows.

var reduceFun2 = function(keyCustomer_address, valuesprice_of_product) {return Array. Sum(valuesprice_of_product);};

MongoDB MapReduce-1.3

3. Perform MapReduce Operation

  • After creating a map and reduce function we have performing MapReduce operation on order_test table are as follows.
  • In the above example, we have performed the operation by using a map and reduce function.
  • We have used map function as mapFun1 and reduce function as reduceFun2 are as follows.

db.order_test.mapReduce(mapFun1, reduceFun2, {out: "Test_MapReduce_example" })

Output-1.4

4. Verify the Result Using Sort

  • We have verified the result of MapReduce command by using sort are as follows. The below example shows that verify the result by using a sort in MongoDB.

db.Test_MapReduce_example.find().sort( { _id: 1 } )

Output-1.5

5. Limit using MapReduce Command

  • In the below example, we have a defining limit using the MapReduce command in MongoDB.

db.Test_MapReduce_example.find().limit (1)
db.Test_MapReduce_example.find().limit (2)

Output-1.6

Conclusion

It is a data processing technique used for large data and the useful aggregated result of large data in MongoDB. Reduce function and map function are the two primary inputs of the MapReduce command in MongoDB. MapReduce command is handy and important in MongoDB.

Recommended Articles

This is a guide to MongoDB MapReduce. Here we also discuss the definition and how does MapReduce command work in MongoDB? Along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. Indexes in MongoDB
  2. MongoDB Limit()
  3. MongoDB Collection
  4. MongoDB create Index
  5. MongoDB Data Types | Examples
  6. Guide to MongoDB Geospatial
  7. MongoDB count | How to Works?

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW