- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MongoDB query to display alternative documents with mapReduce() function and emit even field values
Let us create a collection with documents −
> db.demo636.insert({id:1}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:2}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:3}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:4}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:5}); WriteResult({ "nInserted" : 1 }) > db.demo636.insert({id:6}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo636.find();
This will produce the following output −
{ "_id" : ObjectId("5e9c127b6c954c74be91e6d2"), "id" : 1 } { "_id" : ObjectId("5e9c127e6c954c74be91e6d3"), "id" : 2 } { "_id" : ObjectId("5e9c127f6c954c74be91e6d4"), "id" : 3 } { "_id" : ObjectId("5e9c12816c954c74be91e6d5"), "id" : 4 } { "_id" : ObjectId("5e9c12836c954c74be91e6d6"), "id" : 5 } { "_id" : ObjectId("5e9c12896c954c74be91e6d7"), "id" : 6 }
Following is the query to implement mapReduce() and display only the even values −
> db.demo636.mapReduce( ... function () { ... oddCounter++; ... var id= this._id; ... delete this._id; ... if ( oddCounter % d != 0 ) ... emit(id, this ); ... }, ... function() {}, ... { ... "scope": { "oddCounter": 0, "d": 2 }, ... "out": { "inline": 1 } ... } ... )
This will produce the following output −
{ "results" : [ { "_id" : ObjectId("5e9c127b6c954c74be91e6d2"), "value" : { "id" : 1 } }, { "_id" : ObjectId("5e9c127f6c954c74be91e6d4"), "value" : { "id" : 3 } }, { "_id" : ObjectId("5e9c12836c954c74be91e6d6"), "value" : { "id" : 5 } } ], "timeMillis" : 29, "counts" : { "input" : 6, "emit" : 3, "reduce" : 0, "output" : 3 }, "ok" : 1 }
- Related Articles
- MongoDB query to add up the values of a specific field in documents
- MongoDB query to count the number of array items in documents and display in a new field
- MongoDB query to display documents with a specific name irrespective of case
- MongoDB query to find matching documents given an array with values?
- MongoDB query to update each field of documents in collection with a formula?
- MongoDB query to match documents that contain an array field
- Display the last two values from field with MongoDB
- MongoDB query to get distinct FirstName values from documents
- MongoDB query to collectively match intersection of documents against a field
- MongoDB query to match documents with array values greater than a specific value
- MongoDB query to find documents with specific FirstName and LastName
- MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB query to skip documents
- Ignore first 4 values in MongoDB documents and display the next 3?

Advertisements