我有一个需要映射的mongo ObjectId列表,将一些响应(可能存在或可能不存在)分配给该id,然后根据响应生成可用性平均值。例如,我有一个引用Users
的ObjectId列表:
["5d978d372f263f41cc624727", "5d978d372f263f41cc624728", "5d978d372f263f41cc624729"]
还有Events
的汇总列表,其中包含一个employeeResponses
子文档,如果子文档已响应,则记录其_id
,response
和任何事件{{1 }}:
notes
我需要做的是在[
{
...
employeeResponses: [
{
_id: "5d978d372f263f41cc624727",
response: "I want to work.",
notes: ""
},
{
_id: "5d978d372f263f41cc624728",
response: "Available to work.",
notes: ""
}
]
},
{
...
employeeResponses: [
{
_id: "5d978d372f263f41cc624727",
response: "I want to work.",
notes: ""
},
{
_id: "5d978d372f263f41cc624728",
response: "Prefer not to work.",
notes: ""
}
]
},
{
...
employeeResponses: [
{
_id: "5d978d372f263f41cc624727",
response: "Not available to work.",
notes: ""
},
{
_id: "5d978d372f263f41cc624729",
response: "Not available to work.",
notes: ""
},
]
}
]
数组中将他们的响应(或没有响应)分配给他们的ID:
responses
然后,根据他们的响应,生成成员平均可用性(可用:[
{
"_id": "5d978d372f263f41cc624727",
"responses": ["I want to work.", "I want to work.", "Not available to work."]
},
{
"_id": "5d978d372f263f41cc624728",
"responses": ["Available to work.", "Prefer not to work.", "No response."]
}
{
"_id": "5d978d372f263f41cc624729",
"responses": ["No response.", "No response.", "Not available to work."]
}
]
; 不可用:["I want to work.", "Available to work"]
):
["Prefer not to work.", "Not available to work.", "No response."]
在@mickl的帮助下,我可以aggregate the responses for 1 ObjectId,但是我一直在努力将其扩展为ObjectId列表,而没有映射ID并按用户进行汇总-这似乎非常低效且昂贵。 / p>
答案 0 :(得分:1)
您可以尝试以下方法:
原始示例:https://mongoplayground.net/p/_pDaErcuvkP
https://mongoplayground.net/p/oURNxgNiprl中的调整查询:
db.collection.aggregate([
{
$unwind: {
path: "$employeeResponses",
}
},
{
"$group": {
_id: "$employeeResponses._id",
"availability": {
"$sum": {
"$cond": [
{
"$in": [
"$employeeResponses.response",
[
"Available to work.",
"I want to work."
]
]
},
1,
0
]
}
}
}
}
])