Mongodb mongoose-根据条件从查询中排除嵌入文档
我在用猫鼬。我的数据库中有以下文档:Mongodb mongoose-根据条件从查询中排除嵌入文档,mongodb,mongoose,aggregation-framework,Mongodb,Mongoose,Aggregation Framework,我在用猫鼬。我的数据库中有以下文档: question: { "id": "60ab9bd0d40a362189e842ff", "deletedAt": null "answers": [ { "id": "60ab9e4e58a72f4768c5f63b", "dele
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 }
]}
我将非常感谢任何形式的帮助 使用
$eq
运算符匹配$filter
运算符中的条件
{ $project: {
answers: {
$filter: {
input: "$answers",
as: "answer",
cond: {
$eq: ["$$answer.deletedAt", null]
}
}
}
}
},