使用$pull和$[identifier](mongoDB 3.6)从嵌套数组中删除对象
MongoDB 3.6允许对任何数组中的所有匹配元素执行复杂的数组操作-无论嵌套有多深- 考虑以下关于使用$pull和$[identifier](mongoDB 3.6)从嵌套数组中删除对象,mongodb,Mongodb,MongoDB 3.6允许对任何数组中的所有匹配元素执行复杂的数组操作-无论嵌套有多深- 考虑以下关于调查收集的文档: { "_id": "5a7d86d8fac139e71b0b9f5b", "results": [ { "items": [ { "comments": [ { "id" : "123456", "e
调查收集的文档:
{
"_id": "5a7d86d8fac139e71b0b9f5b",
"results": [
{
"items": [
{
"comments": [
{
"id" : "123456",
"email": "user@email.com",
"comment": "comment 1"
}
]
}
]
}
]
}
我正在尝试使用$pull
和新的$[]
删除基于注释id
的注释
我尝试了下面的命令
,基于:
但它不起作用,因为$pull
需要一个数组值:
无法对非数组值应用$pull
有什么帮助吗?谢谢。试试变体
差不多
db.runCommand({
update: "survey",
updates: [
{
q: {},
u: {
$pull: {
"results.$[].items.$[].comments": { "id": "123456" }
}
}
}
]
})
db.runCommand({
update: "survey",
updates: [
{
q: {},
u: {
$pull: {
"results.$[].items.$[].comments": { "id": "123456" }
}
}
}
]
})