
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Implementing MongoDB map-reduce
Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results.
Let us create a collection with documents −
> db.demo280.insertOne({"CustomerName":"Chris","isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e49116edd099650a5401a62") } > db.demo280.insertOne({"CustomerName":"Mike","isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e491170dd099650a5401a63") } > db.demo280.insertOne({"CustomerName":"David","isMarried":false}); { "acknowledged" : true, "insertedId" : ObjectId("5e491170dd099650a5401a64") } > db.demo280.insertOne({"CustomerName":"Bob","isMarried":true}); { "acknowledged" : true, "insertedId" : ObjectId("5e491171dd099650a5401a65") }
Display all documents from a collection with the help of find() method −
> db.demo280.find();
This will produce the following output −
{ "_id" : ObjectId("5e49116edd099650a5401a62"), "CustomerName" : "Chris", "isMarried" : true } { "_id" : ObjectId("5e491170dd099650a5401a63"), "CustomerName" : "Mike", "isMarried" : false } { "_id" : ObjectId("5e491170dd099650a5401a64"), "CustomerName" : "David", "isMarried" : false } { "_id" : ObjectId("5e491171dd099650a5401a65"), "CustomerName" : "Bob", "isMarried" : true }
Following is the query to implement Mongo DB mapreduce −
> db.demo280.mapReduce( ... function() { emit(this.isMarried,true); }, ... ... function(key, values) {return Array.sum(values)}, { ... query:{isMarried:true}, ... out:"Output" ... } ...)
This will produce the following output displaying total 2 documents (input: 2) matched the query and 2 results were emitted (emit: 2) −
{ "result" : "Output", "timeMillis" : 1241, "counts" : { "input" : 2, "emit" : 2, "reduce" : 1, "output" : 1 }, "ok" : 1 }
- Related Articles
- MongoDB query to fetch random value using Map Reduce concept.
- Government implementing Blockchain to reduce Complacency
- Implementing MongoDB $exists and $ne?
- Implementing String Comparison in MongoDB?
- How do map, reduce and filter functions work in Python?
- Convert 2D array to object using map or reduce in JavaScript
- How to convert array into array of objects using map() and reduce() in JavaScript
- How to re-map the fields of a MongoDB collection?
- How to reduce MongoDB storage space after deleting large amount of data?
- Implementing Photomosaics in Python
- Implementing Stacks in C#
- Implementing Checksum Using Java
- Implementing the 5S Methodology
- MongoDB query to convert an array to a map of documents with n attributes?
- Implementing Priority Sort in JavaScript

Advertisements