【发布时间】:2019-10-30 05:29:01
【问题描述】:
我有一个需要映射的 mongo ObjectId 列表,将一些响应(可能存在也可能不存在)分配给该 id,然后根据响应生成可用性平均值。例如,我有一个引用 Users 的 ObjectId 列表:
["5d978d372f263f41cc624727", "5d978d372f263f41cc624728", "5d978d372f263f41cc624729"]
还有一个 Events 的聚合列表,其中包含一个 employeeResponses 子文档,如果他们已经回复,则记录他们的 _id、response 和任何事件 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: ""
},
]
}
]
我需要做的是在responses 数组中将他们的响应(或没有响应)分配给他们的 id:
[
{
"_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."]
}
]
然后,根据他们的响应,生成成员可用性平均值(available:["I want to work.", "Available to work"];unavailable:["Prefer not to work.", "Not available to work.", "No response."]):
[
{
"_id": "5d978d372f263f41cc624727",
"availability": 66
},
{
"_id": "5d978d372f263f41cc624728",
"availability": 33,
}
{
"_id": "5d978d372f263f41cc624729",
"availability": 0,
}
]
在@mickl 的帮助下,我可以aggregate the responses for 1 ObjectId,但我正在努力将其扩展为 ObjectIds 列表,而不映射 ids 并为每个用户聚合 - 这似乎非常低效且成本高昂。
【问题讨论】:
标签: mongodb mongoose aggregation-framework