在MongoDB聚合管道中引用整个文档 [英] Referencing the whole document in MongoDB Aggregation Pipeline

查看:144
本文介绍了在MongoDB聚合管道中引用整个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用'$'运算符在MongoDB聚合管道中引用属性的各个值.但是,如何访问(参考)整个文档?

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
} 

由于我的收藏集中有数十万条推文,因此我想按"clusters.clusterID"对所有推文进行分组.基本上,我想编写如下查询:

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?

推荐答案

使用 $$ROOT 变量:

引用当前在聚合管道阶段正在处理的根文档,即顶级文档.

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

这篇关于在MongoDB聚合管道中引用整个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆