- 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 Aggregate Second Element from Input Element?
To aggregate second element from input element, use mapReduce(). 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.demo621.insert({ _id: 101, Name1: "John", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 102, Name1: "Bob", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 103, Name1: "Chris", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 104, Name1: "Sam", Name2: "John" }); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo621.find();
This will produce the following output −
{ "_id" : 101, "Name1" : "John", "Name2" : "John" } { "_id" : 102, "Name1" : "Bob", "Name2" : "John" } { "_id" : 103, "Name1" : "Chris", "Name2" : "John" } { "_id" : 104, "Name1" : "Sam", "Name2" : "John" }
Following is the query to aggregate second element from input element −
> db.demo621.mapReduce( ... function () { ... track++; ... var actualId= this._id; ... delete this._id; ... if ( track % div == 0 ) ... emit(actualId, this ); ... }, ... function() {}, ... { ... "scope": { "track": 0, "div": 2 }, ... "out": { "inline": 1 } ... } ... )
This will produce the following output −
{ "results" : [ { "_id" : 102, "value" : { "Name1" : "Bob", "Name2" : "John" } }, { "_id" : 104, "value" : { "Name1" : "Sam", "Name2" : "John" } } ], "timeMillis" : 48, "counts" : { "input" : 4, "emit" : 2, "reduce" : 0, "output" : 2 }, "ok" : 1 }
- Related Articles
- Aggregate a $slice to get an element in exact position from a nested array in MongoDB?
- Get the first element in an array and return using MongoDB Aggregate?
- How to aggregate two lists if at least one element matches in MongoDB?
- Remove null element from MongoDB array?
- How to find the second largest element in a user-input JavaScript array?
- How to exclude first and second element from jQuery selector?
- Fetch Second minimum element from an array without sorting JavaScript
- Select every element that is the second element of its parent, counting from the last child
- Removing an array element from a MongoDB collection
- How to get a particular element from MongoDB array?
- MongoDB query to pull array element from a collection?
- How to delete element from an array in MongoDB?
- Unset an attribute from a single array element in MongoDB?
- Extract a particular element from a nested array in MongoDB
- MongoDB query to match and remove element from an array?

Advertisements