根据4J41的提议(感谢他/她),我将previous question分成了一个新的。
我的mongoDB查询最后一个问题。我很乐意得到一些帮助...
在我的MongoCollection下面:
请注意,KLLS
可以是a
,b
或c
,还有3 types
:processus
,work
,{ {1}},对某些人来说,viewing
{仅适用于key
)。
work
目前,查询是(再次感谢4J41用户):
{_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" }
...
我在这里修改了一些订单/措辞以使其更清晰
此时我得到了:
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" }
]
}]
...
。目前,我有一个由Work
条记录组成的列表(4
:_id
,3
,4
,5
)。往上看。 我希望6
下面的小组如下(警告:我不知道key
的{{1}}。
value
我尝试过(1)一种嵌套的key
和(2)将条件添加到第一个$ group中以制作复合[ { _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" }
]
]
...
,但它似乎不起作用(空)查询结果)。
你知道如何解决这个问题吗? 感谢。
答案 0 :(得分:0)
您可以添加嵌套的$cond
来过滤键123或456.然后,最后的$project
阶段可用于构建数组。
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}}
])