

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- MongoDB query to fetch random value using Map Reduce concept.
- Implementing MongoDB $exists and $ne?
- Implementing String Comparison in MongoDB?
- 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 Stacks in C#
- Implementing Photomosaics in Python
- Implementing Checksum Using Java
- Implementing Priority Sort in JavaScript
- Implementing Linear Search in JavaScript
- Implementing counting sort in JavaScript
- Implementing block search in JavaScript
- Implementing models reversion in Django
Advertisements