MongoDB聚合`$ group`和数组名称

时间:2016-10-09 19:05:03

标签: mongodb aggregation-framework

我的mongoDB查询存在一个问题(使用aggregate)。 我很乐意得到一些线索......

在我的MongoCollection下面:
请注意,KLLS可以是abc,还有3 typesprocessuswork和{ {1}}。

viewing

我想通过查询实现的目标:

{_id: 1, KLLS: "a", action: "A", type: "Processus", date: Date, other:"abc" }
{_id: 2, KLLS: "b", action: "B", type: "Processus", date: Date }
{_id: 3, KLLS: "a", action: "C", type: "Work"     , date: Date, other:"xyz" }
{_id: 4, KLLS: "b", action: "D", type: "Work"     , date: Date }
{_id: 5, KLLS: "a", action: "E", type: "Viewing"  , date: Date }
{_id: 6, KLLS: "b", action: "F", type: "Viewing"  , date: Date }
...

此时,我的(失败)查询是:

[ { 
    _id: { KLLS: "a"},
    Processus: [[action: "A", date: "Date", other: "abc"]]
    Work:      [[action: "C", date: "Date"]]
    Viewing:   [[action: "E", date: "Date"]]
  },
  { 
    _id: { KLLS: "b"},
    Processus: [[action: "B", date: "Date"]]
    Work:      [[action: "D", date: "Date", other: "xyz"]]
    Viewing:   [[action: "F", date: "Date"]]
  }
]

但是,我有一个问题:它产生db.collection('events').aggregate([ {$match: { KLLS: {$in: something} }}, {$group: { _id: { klls: "$KLLS", type: "$type" }, array: { $push: "$$ROOT" } }} ]) 但我没有3个名字很好的composite id ......

arrays

你知道如何解决这个问题吗? 感谢。

********************编辑********************** < /强>

让我们假设同样的集合也包含:

[
 {   
     _id: { KLLS: "a", type: "Processus" },
     array: [[action: "A", date: "Date", other: "abc"]]
 },
 { 
     _id: { KLLS: "a", type: "Work" },
     array: [[action: "C", date: "Date"]]
 },
 { 
     _id: { KLLS: "a", type: "Viewing" },
     array: [[action: "E", date: "Date"]]
 }, 
 { 
     _id: { KLLS: "b", type: "Processus" },
     array: [[action: "B", date: "Date"]]
 },
 { 
     _id: { KLLS: "b", type: "Work" },
     array: [[action: "D", date: "Date", other: "xyz"]]
 }, 
 { 
     _id: { KLLS: "b", type: "Viewing" },
     array: [[action: "F", date: "Date"]]
 },
 ...
]

(请注意,对于类型{_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" } ... ,我有另一个Work)。

感谢您的帮助,此时此刻,我得到了:

key

然而,我想要的最终结果是:

[{ 
    _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: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" }
        {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" }
    ]
...

我尝试了(1)一种[{ _id: { KLLS: "a"}, Processus: [ everything is ok ], Viewing: [ everything is ok ], Details: [ everything is ok ], Work: [ subArrayByKey [ // key = 123 {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"123" }, {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "123" } ], subArrayByKey [ // key = 456 {_id: 3, KLLS: "a", action: "AB", type: "Work", date: Date, key:"456" }, {_id: 4, KLLS: "b", action: "XY", type: "Work", date: Date, key: "456" } ] ] ... 以及(2)将标准添加到您的第一个$组中以制作nested $group(就像我在开始时尝试过的那样)。但composite id使我感到尴尬......

$eq

最后,并不奇怪,我失败了...返回查询不是空的.. 如果我可以..你有任何线索......; - )

1 个答案:

答案 0 :(得分:0)

您可以将$cond运算符与$push一起使用,以便为符合条件的数组添加值。

$cond是三元运算符,在下面的示例中,当满足条件时,原始文档将添加到数组中,并且当条件不满足时,将虚拟值false添加到数组中

在最后的$project阶段,使用false运算符删除数组中的虚拟setDifference条目。

db.events.aggregate([
    {"$group": {
         "_id" : "$KLLS",
         "Processus": {
             "$push": { 
                 "$cond": [{"$eq": ["$type", "Processus"]}, "$$ROOT", false]
             }
         },
         "Work": {
             "$push": {
                 "$cond": [{"$eq": ["$type","Work"]}, "$$ROOT", false]
             }
         },
         "Viewing": {
             "$push": {
                 "$cond": [{"$eq" : ["$type","Viewing"]}, "$$ROOT", false]
             }
         },
         "Details": {"$push" : "$other"}
    }},

    {"$project": {
         "_id": 0, 
         "KLLS": "$_id",
         "Processus": {"$setDifference": ["$Processus", [false]]},
         "Work":      {"$setDifference": ["$Work",      [false]]},
         "Viewing":   {"$setDifference": ["$Viewing",   [false]]},
         "Details": 1
    }}
])

此外,要向数组中添加少量字段,您可以尝试以下内容。

db.events.aggregate([
    {"$group": {
         "_id": "$KLLS",
         "Processus": {
             "$push": { 
                 "$cond": [{"$eq": ["$type","Processus"]}, {"action": "$action","other": "$other"}, false]
             }
          },
          "Work": {
              "$push": {
                  "$cond": [{"$eq": ["$type","Work"]}, {"action": "$action","other": "$other"}, false]
              }
          },
          "Viewing":{
              "$push": {
                  "$cond": [{"$eq": ["$type","Viewing"]}, {"action": "$action","other": "$other"}, false]
              }
          },
          "Details": {"$push": "$other"}
    }},

    {"$project": { 
         "_id": 0, 
         "KLLS": "$_id",
         "Processus": {"$setDifference": ["$Processus", [false]]},
         "Work":      {"$setDifference": ["$Work",      [false]]},
         "Viewing":   {"$setDifference": ["$Viewing",   [false]]},
         "Details": 1
    }}
])