问题描述
MongoDB 3.6允许对任何数组中的所有匹配元素执行复杂的数组操作-无论嵌套深度如何-使用$ [identifier]
MongoDB 3.6 allow to perform complex array manipulations against all matching elements in any array – no matter how deeply nested – Update Nested Arrays with $[identifier]
请考虑以下survey
集合上的文档:
Consider the document below on survey
collection:
{
"_id": "5a7d86d8fac139e71b0b9f5b",
"results": [
{
"items": [
{
"comments": [
{
"id" : "123456",
"email": "[email protected]",
"comment": "comment 1"
}
]
}
]
}
]
}
我正在尝试使用$pull
和新的$[<identifier>]
I'm trying to remove a comment based on comment id
using $pull
and the new $[<identifier>]
我根据更新命令在下面尝试了command
:
I tried the command
below based on The update command :
db.runCommand({
update: "survey",
updates: [
{
q: {},
u: {
$pull: {
"results.$[].items.$[].comments.$[comment]": { "comment.id": { $eq: "123456" } }
}
},
arrayFilters: [{ "comment.id": { $eq: "123456" } }]
}
]
})
但是它不起作用,因为$pull
期望一个数组值:Cannot apply $pull to a non-array value
But it didn't work because $pull
expect an array value:Cannot apply $pull to a non-array value
有什么帮助吗?谢谢.
推荐答案
尝试 positional all
$ [] 变体.
Try positional all
$[] variant.
类似
db.runCommand({
update: "survey",
updates: [
{
q: {},
u: {
$pull: {
"results.$[].items.$[].comments": { "id": "123456" }
}
}
}
]
})
这篇关于使用$ pull和$ [identifier]从嵌套数组中删除对象(mongoDB 3.6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!