Mongodb-$ group中的$ group(通过“键”) [英] Mongodb - $group inside a $group (by 'key')

查看:67
本文介绍了Mongodb-$ group中的$ group(通过“键”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 4J41 的建议(感谢他/她),我拆分了上一个问题变成一个新问题。

As proposed by 4J41 (thanks to him/her), I split my previous question into a new one.

我有我的mongoDB查询的最后一个问题。我很高兴得到一些帮助...

I have one last problem with my mongoDB query. And I would be delighted to get some help...

在我的MongoCollection下面:
请注意, KLLS a b c ,并且有3种类型 processus work 查看,还有一些特殊的(仅用于工作)。

Below my MongoCollection : Note that KLLS is either a, b or c, and there are 3 types : processus, work, viewing and, for some, a special key(only for work).

{_id: 1, KLLS: "a", action: "A", type: "Processus", date: Date }
{_id: 2, KLLS: "b", action: "B", type: "Processus", date: Date }
{_id: 5, KLLS: "a", action: "E", type: "Viewing"  , date: Date }
{_id: 6, KLLS: "b", action: "F", type: "Viewing"  , date: Date }
...
{_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"123" }
{_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "123" }
{_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" }
{_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" }
...

当前,查询为(再次感谢4J41用户):

db.collection('events').aggregate([
   { $match: { KLLS: {$in: something} } },
   { $sort: { date: 1 } },
   { '$group': {
     '_id': '$KLLS',
     'Processus': {'$push': {'$cond': [{'$eq': ['$type', 'Processus']}, '$$ROOT', false]}},
     'Works': {'$push': {'$cond': [{'$eq': ['$type', 'Works']}, '$$ROOT', false]}},
     'Viewings': {'$push': {'$cond': [{'$eq': ['$type', 'Viewings']}, '$$ROOT', false]}},
     'Details': {'$push': {'$cond': [{'$eq': ['$action', 'Stuff']}, '$$ROOT', false]}}
          }},
    {'$project': {
      '_id': 0,
      'KLLS': '$_id',
      'Processus': { '$setDifference': ['$Processus', [false]] },
      'Works': { '$setDifference': ['$Works', [false]] },
      'Viewings': { '$setDifference': ['$Viewings', [false]] },
      'Details': { '$setDifference': ['$Details', [false]] }
          }}
        ]
      )






我从这里编辑,更改了一些顺序/措辞以使其更清晰

这时我得到了:

[ { _id: { KLLS: 'a'},
  Processus: [   everything is ok    ]
  Viewing:   [   everything is ok    ]
  Details:   [   everything is ok    ]
  Work:      [
   {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"123" }
   {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "123" }
   {_id: 5, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" }
   {_id: 6, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" }
            ]
  }]
...

唯一的最后一个问题是工作。目前,我有一个由 4 个记录组成的列表( _id 3 4 5 6 )。往上看。 我想通过如下所示的来获得子组(注意:我不知道的值键的c $ c> )。

The only last problem is into Work. Currently, I have a list composed of 4 records (_id: 3, 4, 5, 6). See above. Whereas I'd like to get subgroups by key like below (caution: I do not know the value of the key).

[ { _id: { KLLS: 'a'},
  Processus: [ everything is ok ]
  Viewing:   [ everything is ok ]
  Details:   [   everything is ok    ]
  Work:      [
     [ // this a "$group" by key 
         {_id: ..., KLLS: "...", action: "...", type: "Work", date: Date, key:"123" }
         {_id: ..., KLLS: "...", action: "...", type: "Work", date: Date, key: "123" }
     ]
     [ // this a "$group" by key 
         {_id: ..., KLLS: "...", action: "...", type: "Work", date:..., key:"456" }
         {_id: ..., KLLS: "...", action: "...", type: "Work", date:..., key: "456" }
     ]
]
...

我尝试过(1)一种嵌套的 $ group ,还(2)将条件添加到第一个$ group中以组成复合 id ,但这似乎不起作用(空查询结果)。

I tried doing (1) a kind of nested $group and also (2) to add criteria into first $group to make a composite id, but it doesn't seem to work (empty query result).

您知道如何解决吗?
谢谢。

Do you have any idea how to resolve this? Thanks.

推荐答案

您可以添加嵌套的 $ cond 来过滤键123或456。然后,最后的 $ project 阶段可以用来构造数组。

You can add nested $condto filter the keys 123 or 456. Then, a final $project stage can be used to construct the array.

db.events.aggregate([
    {"$group":
        {   "_id":"$KLLS",
            "Processus":{"$push":{"$cond":[{"$eq":["$type","Processus"]},'$$ROOT',false]}},
            "Work123":
                {"$push":
                    {"$cond":
                        [
                            {"$eq":["$type","Work"]},
                            {"$cond":
                                [
                                    {"$eq":["$key","123"]},
                                    '$$ROOT',
                                    false
                                ]
                            },
                            false
                        ]
                    }
                },
            "Work456":
                {"$push":
                    {"$cond":
                        [
                            {"$eq":["$type","Work"]},
                            {"$cond":
                                [
                                    {"$eq":["$key","456"]},
                                    '$$ROOT',
                                    false
                                ]
                            },
                            false
                        ]
                    }
                },
            "Viewing":{"$push":{"$cond":[{"$eq":["$type","Viewing"]},'$$ROOT',false]}}
        }
    },
    {"$project": { "_id":0, "KLLS":"$_id", "Processus":{"$setDifference":["$Processus",[false]]},
        "123":{"$setDifference":["$Work123",[false]]},
        "456":{"$setDifference":["$Work456",[false]]},
        "Viewing":{"$setDifference":["$Viewing",[false]]}
        }
    },
    {"$project": { "KLLS":1, "Processus":1, "Work" : [{"123" : "$123"}, {"456" : "$456"}],"Viewing":1}}
])

这篇关于Mongodb-$ group中的$ group(通过“键”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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