Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing the whole document in MongoDB Aggregation Pipeline

I can reference the values of individual values of attributes in MongoDB aggregation pipeline using '$' operator. But, how do I access (reference) the whole document ?


UPDATE: An example provided to explain scenario.

Here's an example of what I'm trying to do. I have a collection of tweets. And every tweet has a member 'clusters', which is an indication of to what cluster a particular tweet belongs to.

{
    "_id" : "5803519429097792069",
    "text" : "The following vehicles/owners have been prosecuted by issuing notice on the basis of photographs on dated... http://t.co/iic1Nn85W5",
    "oldestts" : "2013-02-28 16:11:32.0",
    "firstTweetTime" : "4 hours ",
    "id" : "307161122191065089",
    "isLoc" : true,
    "powertweet" : true,
    "city" : "new+delhi",
    "latestts" : "2013-02-28 16:35:05.0",
    "no" : 0,
    "ts" : 1362081807.9693,
    "clusters" : [
        {
            "participationCoeff" : 1,
            "clusterID" : "5803519429097792069"
        }
    ],
    "username" : "dtptraffic",
    "verbSet" : [
        "date",
        "follow",
        "prosecute",
        "have",
        "be"
    ],
    "timestamp" : "4 hours ",
    "entitySet" : [ ],
    "subCats" : {
        "Generic" : [ ]
    },
    "lang" : "en",
    "fns" : 18.35967,
    "url" : "url|109|131|http://fb.me/2CeaI7Vtr",
    "cat" : [
        "Generic"
    ],
    "order" : 7
} 

Since, there are some couple of hundred thousands tweets in my collection, I want to group all tweets by 'clusters.clusterID'. Basically, I would want to write a query like following:

db.tweets.aggregate (
{ $group : { _id : '$clusters.clusterID', 'members' : {$addToSet : <????> } } }
)

I want to access the presently processing document and reference it where I have put in the above query. Does anyone knows how to do this?

like image 284
VaidAbhishek Avatar asked Feb 28 '13 19:02

VaidAbhishek


People also ask

Which aggregation pipeline stage reshapes each document in the stream such as by adding new fields or removing existing fields?

To use the $out stage, it must be the last stage in the pipeline. Returns plan cache information for a collection. Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document.

How many stages must the aggregate function include in the aggregation pipeline?

Here, the aggregate() function is used to perform aggregation it can have three operators stages, expression and accumulator.

Can we update in aggregate MongoDB?

Starting in MongoDB 4.2, you can use the aggregation pipeline for update operations. With the update operations, the aggregation pipeline can consist of the following stages: $addFields. $set.


1 Answers

Use the $$ROOT variable:

References the root document, i.e. the top-level document, currently being processed in the aggregation pipeline stage.

like image 200
Volox Avatar answered Oct 05 '22 22:10

Volox