mongodb汇总嵌入式文档值 [英] mongodb aggregate embedded document values

查看:74
本文介绍了mongodb汇总嵌入式文档值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Im Strugling在mongodb中具有一些聚合功能.

Im Strugling with some aggregation functions in mongodb.

说我有一些这样的文件

 [
 {
    _id: "1",
    periods: [
      {
         _id: "12",
         tables: [
           {
              _id: "121",
              rows: [
                  { _id: "1211", text: "some text"},
                  { _id: "1212", text: "some other text"},
                  { _id: "1213", text: "yet another text"},

              ]
           }
         ]
      },
      {
         _id: "13",
         tables: [
           {
              _id: "131",
              rows: [
                  { _id: "1311", text: "different text"},
                  { _id: "1312", text: "Oh yeah"}                      
              ]
           }
         ]
      }
    ]
 },
 {
    _id: "2",
    periods: [
      {
         _id: "21",
         tables: [
           {
              _id: "212",
              rows: [
                  { _id: "2121", text: "period2 text"},
                  { _id: "2122", text: "period2 other text"},
                  { _id: "2123", text: "period2 yet another text"},

              ]
           }
         ]
      }
    ]
 }
 ]

现在,我想使用mongodb查询来检索一个特定顶级商品的所有唯一文本.

Now I want to use a mongodb query to retrieve all unique texts for one specific top level item.

例如,汇总顶部_id 1的所有文本.这意味着我要获取两个周期子树中的所有文本.

eg agregate all texts for the top _id 1. This would mean that I want to get all the texts in both of the period subtrees.

预期输出如下:

汇总对_id:1进行过滤的文本

aggregate texts filtering on _id: 1

[
   "some text",
   "some other text",
   "yet another text",
   "different text",
   "Oh yeah"
]

汇总对_id:2进行过滤的文本

aggregate texts filtering on _id: 2

[
  "period2 some text",
  "period2 some other text",
  "period2 yet another text"
]

到目前为止,我已经设法汇总了所有文本,但是最终以多个数组的形式出现,并且我还没有使用$ match过滤ID上的文本,

So far I have managed to aggregate all the texts , but the end up in multiple arrays and I have not managed to filter them on the id using $match,

我当前的汇总查询如下

[ 
    { "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
    { "$unwind" : "$text"},
    { "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
    { "$project" : { "_id" : 0 , "texts" : 1}} 
]

它给我带来了类似这样的结果

It gives me a result loooking something like this

{ "texts" : [ 
        [ [ "Some text" , "Some other text" , "yet another text"] , [ "different text" , "oh yeah" ] ],
        [ [ "period2 some text", "period2 some other text", "period2 yet another text"]]
    ]}

如果我添加$ match:{_id:1},则不会返回任何结果.

If I add $match: {_id: 1}, no results are returned.

任何人都可以帮我解决这个问题,或为我指出解决方法的方向.我一直在寻找资源,但是似乎找不到关于如何使用这些聚合函数的任何好的文档. mongodb文档仅使用简单的文档.

Can anyone please help me out with this one, or point me in a direction on how to solve it. I've been searching for resources, but doesnt seem to find any good documentation on how to use these aggregate functions. The mongodb docs only use simple documents.

PS,我知道我可以使用mapreduce做到这一点,但希望能够为此使用聚合函数.

PS I know I can do this using mapreduce, but was hoping to be able to use an aggregate function for this.

推荐答案

展开仅会下降一级,因此您必须像调用

Unwind only goes down one level, so you have to call as many times as many levels you have if you do it like

[ 
    { "$project" : { "text" : "$periods.tables.rows.text" , "_id" : "$_id"}},
    { "$unwind" : "$text"},
    { "$unwind" : "$text"},
    { "$unwind" : "$text"},
    { "$group" : { "_id" : "$_id" , "texts" : { "$addToSet" : "$text"}}},
    { "$project" : { "_id" : 0 , "texts" : 1}} 
]

它将按您期望的那样工作.

It will work as you expect.

这篇关于mongodb汇总嵌入式文档值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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