从嵌套数组 mongodb 中删除元素 [英] Remove element from nested array mongodb
问题描述
我有以下文档,它有两个数组,一个在另一个里面,
附件数组和附件数组中的文件数组.我想使用这个元素 _id 删除 files 数组中的一个元素.但它不适合我,我试过这个代码,它返回{ 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 或 4.0,具体取决于您的版本} )
.
Use db.adminCommand( { setFeatureCompatibilityVersion: 3.6 or 4.0 depending on your version } )
if your are upgrading from old version.
这篇关于从嵌套数组 mongodb 中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!