【发布时间】:2021-05-29 15:49:03
【问题描述】:
我正在使用猫鼬。我的数据库中有以下文档:
question: {
"id": "60ab9bd0d40a362189e842ff",
"deletedAt": null
"answers": [
{
"id": "60ab9e4e58a72f4768c5f63b",
"deletedAt": "2021-05-28T20:23:30.409Z",
},
{
"id": "60ab9e4e58a72f4768c5f64c",
"deletedAt": null,
},
}
}
我想获得deletedAt 字段为空的所有问题,以及deletedAt 字段为空的所有答案。
所以我的查询结果应该是这样的:
question: {
"id": "60ab9bd0d40a362189e842ff",
"deletedAt": null
"answers": [
{
"id": "60ab9e4e58a72f4768c5f64c",
"deletedAt": null,
},
}
}
我尝试过的:
Question.aggregate([
{ $match: { deletedAt: null }},
{ $project: {
answers: {
$filter: {
input: "$answers",
as: "answer",
cond: { "$$answer.deletedAt": null }
}
}
}
},
{ $sort: sort },
{ $limit: limit + 1 }
]}
如果能提供任何帮助,我将不胜感激!
【问题讨论】:
标签: mongodb mongoose aggregation-framework