
- 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
Apply the Group Accumulator Operator $first on the system variable $$ROOT to return reference to the root document?
The accumulators are operators that maintain their state as documents progress through the pipeline.
The $ROOT references the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.
Let us create a collection with documents −
> db.demo582.insertOne({FirstName:"Chris",Age:21,createDate:new ISODate("2020-01-10")});{ "acknowledged" : true, "insertedId" : ObjectId("5e91ce41fd2d90c177b5bcbd") } > db.demo582.insertOne({FirstName:"Chris",Age:21,createDate:new ISODate("2020-04-21")});{ "acknowledged" : true, "insertedId" : ObjectId("5e91ce4ffd2d90c177b5bcbe") } > db.demo582.insertOne({FirstName:"Chris",Age:22,createDate:new ISODate("2020-02-11")});{ "acknowledged" : true, "insertedId" : ObjectId("5e91ce59fd2d90c177b5bcbf") } > db.demo582.insertOne({FirstName:"Chris",Age:22,createDate:new ISODate("2020-01-12")});{ "acknowledged" : true, "insertedId" : ObjectId("5e91ce6efd2d90c177b5bcc0") }
Display all documents from a collection with the help of find() method −
> db.demo582.find();
This will produce the following output −
{ "_id" : ObjectId("5e91ce41fd2d90c177b5bcbd"), "FirstName" : "Chris", "Age" : 21, "createDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e91ce4ffd2d90c177b5bcbe"), "FirstName" : "Chris", "Age" : 21, "createDate" : ISODate("2020-04-21T00:00:00Z") } { "_id" : ObjectId("5e91ce59fd2d90c177b5bcbf"), "FirstName" : "Chris", "Age" : 22, "createDate" : ISODate("2020-02-11T00:00:00Z") } { "_id" : ObjectId("5e91ce6efd2d90c177b5bcc0"), "FirstName" : "Chris", "Age" : 22, "createDate" : ISODate("2020-01-12T00:00:00Z") }
Following is the query to apply the Group Accumulator operator −
> db.demo582.aggregate([ ... { ... "$group": { ... "_id": "$FirstName", ... "MaximumDate": { ... "$max": "$createDate" ... }, ... "count": { ... "$sum": 1 ... }, ... "details": { ... "$first": "$$ROOT" ... } ... } ... }, ... { ... "$project": { ... "MaximumDate": 1, ... "count": 1, ... "details": { ... "_id": "$_id", ... "FirstName": "$details.FirstName", ... "Age" : "$details.Age", ... } ... } ... } ... ])
This will produce the following output −
{ "_id" : "Chris", "MaximumDate" : ISODate("2020-04-21T00:00:00Z"), "count" : 4, "details" : { "_id" : "Chris", "FirstName" : "Chris", "Age" : 21 } }
- Related Articles
- How to define the root of an HTML document?
- Style the document's root element with CSS
- What is Root System?
- How to return the id of the first image in a document with JavaScript?
- Difference between Root System and Shoot System.
- Return the cube-root of an array elementwise in Numpy
- 8086 program to find the square root of a perfect square root number
- Babylonian method to find the square root
- How to apply the aggregation list on every group of pandas DataFrame?
- How to change the root directory of an Apache server on Linux?
- How to apply XSL transformation on an XML document?
- Program to print the first shortest root to leaf path in a Binary Tree using C++
- Using square root table, find the square root of the following (up to 2 decimal places)$ 83.17$
- Print the first shortest root to leaf path in a Binary Tree in C++ Programming.
- Reset the root user password on rhel7 xcentos7 x

Advertisements