Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is $$ROOT in MongoDB aggregate and how it works?

I am watching a tutorial I can understand how this aggregate works, What is the use of pings, $$ROOT in it.

client = pymongo.MongoClient(MY_URL)
pings = client['mflix']['watching_pings']
cursor = pings.aggregate([
  {
    "$sample": { "size": 50000 }
  },
  {
    "$addFields": { 
      "dayOfWeek": { "$dayOfWeek": "$ts" },
      "hourOfDay": { "$hour": "$ts" }
    }
  },
  {
    "$group": { "_id": "$dayOfWeek", "pings": { "$push": "$$ROOT" } }
  },
  {
    "$sort": { "_id": 1 }
  }
]);
like image 396
Saif Haider Avatar asked May 14 '20 18:05

Saif Haider


People also ask

What is $$ root in MongoDB?

The $$ROOT variable contains the source documents for the group. If you'd like to just pass them through unmodified, you can do this by $pushing $$ROOT into the output from the group.

How does aggregate work in MongoDB?

In MongoDB, aggregation operations process the data records/documents and return computed results. It collects values from various documents and groups them together and then performs different types of operations on that grouped data like sum, average, minimum, maximum, etc to return a computed result.

Why dollar is used in MongoDB?

According to the docs, a "$" is reserved for operators. If you look at the group operator however, values need to have a dollar prefixed. These values are not operators.

What is replace root in MongoDB?

Definition. Replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. You can promote an existing embedded document to the top level, or create a new document for promotion (see example).


1 Answers

Let's assume that our collection looks like below:

{
    "_id" : ObjectId("b9"),
    "key" : 1,
    "value" : 20,
    "history" : ISODate("2020-05-16T00:00:00Z")
},
{
    "_id" : ObjectId("ba"),
    "key" : 1,
    "value" : 10,
    "history" : ISODate("2020-05-13T00:00:00Z")
},
{
    "_id" : ObjectId("bb"),
    "key" : 3,
    "value" : 50,
    "history" : ISODate("2020-05-12T00:00:00Z")
},
{
    "_id" : ObjectId("bc"),
    "key" : 2,
    "value" : 0,
    "history" : ISODate("2020-05-13T00:00:00Z")
},
{
    "_id" : ObjectId("bd"),
    "key" : 2,
    "value" : 10,
    "history" : ISODate("2020-05-16T00:00:00Z")
}

Now based on the history field you want to group and insert the whole documents in to an array field 'items'. Here $$ROOT variable will be helpful.

So, the aggregation query to achieve the above will be:

db.collection.aggregate([{
    $group: {
        _id: '$history',
        items: {$push: '$$ROOT'}
    }
}])

It will result in following output:

{
    "_id" : ISODate("2020-05-12T00:00:00Z"),
    "items" : [
        {
            "_id" : ObjectId("bb"),
            "key" : 3,
            "value" : 50,
            "history" : ISODate("2020-05-12T00:00:00Z")
        }
    ]
},
{
    "_id" : ISODate("2020-05-13T00:00:00Z"),
    "items" : [
        {
            "_id" : ObjectId("ba"),
            "key" : 1,
            "value" : 10,
            "history" : ISODate("2020-05-13T00:00:00Z")
        },
        {
            "_id" : ObjectId("bc"),
            "key" : 2,
            "value" : 0,
            "history" : ISODate("2020-05-13T00:00:00Z")
        }
    ]
},
{
    "_id" : ISODate("2020-05-16T00:00:00Z"),
    "items" : [
        {
            "_id" : ObjectId("b9"),
            "key" : 1,
            "value" : 20,
            "history" : ISODate("2020-05-16T00:00:00Z")
        },
        {
            "_id" : ObjectId("bd"),
            "key" : 2,
            "value" : 10,
            "history" : ISODate("2020-05-16T00:00:00Z")
        }
    ]
}

I hope it helps.

like image 118
ngShravil.py Avatar answered Oct 21 '22 10:10

ngShravil.py