我有一个嵌套很深的家庭成员文档
[{
"id": 1,
"children" : [
{
"id" : 1,
"grandChildren": [
{
"id" : 1,
"toys": [
{
"id":1
}
]
}
]
},
{
"id" : 2,
"grandChildren": [
{
"id" : 1,
"toys": [
{
"id":1
}
]
}
]
}
]
}]
我被告知这些玩具是孙子孙女独有的,也是独一无二的,等等,但它们可能不是,我想绝对确保当我移除它们时,我只是为特定孩子的特定孙子等移除它们。
我有一个查询,可以拉出所有的玩具匹配的id我想知道我如何才能更具体,通过特定的父母挖洞?
db.family.updateOne(
{"id": "1"},
{"$pull" :
{ "children.$[].grandChildren.$[].toys" :
{"id" : "1"}
}
})
感谢所有的帮助
发布于 2021-02-10 18:33:47
您想要做的是像这样使用arrayFilters:
db.collection.updateOne({
"id": "1"
},
{
"$pull": {
"children.$[child].grandChildren.$[grandChild].toys": {
"id": "1"
}
}
},
{
arrayFilters: [
{
"child.id": "1"
},
{
"grandChild.id": "1"
}
]
})
https://stackoverflow.com/questions/66134247
复制相似问题