在MongoDB聚合管道中引用整个文档

编程入门 行业动态 更新时间:2024-10-21 05:38:15
本文介绍了在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... 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|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聚合管道中引用整个文档

本文发布于:2023-11-22 20:40:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618937.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:道中   文档   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!