Updated February 28, 2023
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.
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 ()
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);};
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);};
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" })
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 } )
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)
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 –