Apply the Group Accumulator Operator $first on the system variable ROOT to return reference to the root document?

The $first group accumulator operator combined with the $$ROOT system variable returns a reference to the first complete document encountered in each group. The $$ROOT variable references the entire root document being processed in the aggregation pipeline.

Syntax

db.collection.aggregate([
    {
        $group: {
            _id: "$groupingField",
            firstDocument: { $first: "$$ROOT" }
        }
    }
])

Sample Data

Let us create a collection with sample documents −

db.demo582.insertMany([
    {FirstName:"Chris", Age:21, createDate:new ISODate("2020-01-10")},
    {FirstName:"Chris", Age:21, createDate:new ISODate("2020-04-21")},
    {FirstName:"Chris", Age:22, createDate:new ISODate("2020-02-11")},
    {FirstName:"Chris", Age:22, createDate:new ISODate("2020-01-12")}
]);
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5e91ce41fd2d90c177b5bcbd"),
        ObjectId("5e91ce4ffd2d90c177b5bcbe"),
        ObjectId("5e91ce59fd2d90c177b5bcbf"),
        ObjectId("5e91ce6efd2d90c177b5bcc0")
    ]
}

Display all documents from the collection −

db.demo582.find();
{ "_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") }

Example: Using $first with $$ROOT

Apply the group accumulator operator to get the first document reference along with aggregated data −

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"
            }
        }
    }
])
{ "_id" : "Chris", "MaximumDate" : ISODate("2020-04-21T00:00:00Z"), "count" : 4, "details" :
    { "_id" : "Chris", "FirstName" : "Chris", "Age" : 21 }
}

How It Works

  • The $group stage groups documents by FirstName
  • $first: "$$ROOT" captures the complete first document from each group
  • $max finds the maximum date and $sum counts documents
  • The $project stage reshapes the output to show selected fields from the stored document

Conclusion

The $first operator with $$ROOT is useful for capturing complete document references while performing group aggregations. It preserves the first document encountered in each group alongside calculated aggregate values.

Updated on: 2026-03-15T03:44:52+05:30

233 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements