从嵌套数组mongodb中删除元素 [英] Remove element from nested array mongodb
问题描述
我有以下文档,它有两个数组,一个在另一个数组之内,
附件数组中的附件数组和文件数组.
我想使用此元素_id删除文件数组中的一个元素.但是它对我不起作用,我尝试了这段代码,它返回
{n:144,nModified:0,ok:1}
i have the following document , it has two array's , one inside the other ,
attachment array and files array inside attachment array .
i want to delete an element inside files array using this element _id . but its not working with me , i tried this code , it return
{ n: 144, nModified: 0, ok: 1 }
Invoice.update({}, {
$pull: {
"attachment":
{
"files":
{
$elemMatch:
{ _id: ObjectId("5b7937014b2a961d082de9bf") }
}
}
}
}, { multi: true })
.then(result => {
console.log("delete", result);
});
这是文档的外观
推荐答案
您可以在下面的3.6版本中尝试更新查询.
You can try below update query in 3.6 version.
Invoice.update(
{},
{"$pull":{"attachment.$[].files":{_id:ObjectId("5b7969ac8fb15f3e5c8e844e")}}},
{"multi": true}, function (err, result) {console.log(result);
});
如果要从旧版本升级,请使用db.adminCommand( { setFeatureCompatibilityVersion: 3.6 or 4.0 depending on your version } )
.
Use db.adminCommand( { setFeatureCompatibilityVersion: 3.6 or 4.0 depending on your version } )
if your are upgrading from old version.
这篇关于从嵌套数组mongodb中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!