【发布时间】:2019-01-25 17:39:47
【问题描述】:
我有以下文档,它有两个数组,一个在另一个里面,
附件数组中的附件数组和文件数组。
我想使用这个元素 _id 删除文件数组中的一个元素。但它不适合我,我尝试了这段代码,它返回
{ n: 144, nModified: 0, ok: 1 }
Invoice.update({}, {
$pull: {
"attachment":
{
"files":
{
$elemMatch:
{ _id: ObjectId("5b7937014b2a961d082de9bf") }
}
}
}
}, { multi: true })
.then(result => {
console.log("delete", result);
});
【问题讨论】:
-
@AnthonyWinzlet 谢谢,但帖子不同,我有 2 个数组,我想删除第一个数组中存在的第二个数组中的元素。
-
试试
Invoice.update( {}, { "$pull": { "attachment.$[].files":{_id:ObjectId("5b7969ac8fb15f3e5c8e844e") } }}, { "multi": true } , function (err, result) { console.log(result); }); -
{ ok: 0, n: 0, nModified: 0 } { MongoError: cannot use the part (attachment of attachment.$[].files) 遍历元素 ({attachment: []} )
-
你的 mongo 服务器版本是多少?你需要3.6。此外,您可能需要从 shell
db.adminCommand( { setFeatureCompatibilityVersion: 3.6 or 4.0 depending on your version } )运行管理命令 -
MongoDB shell 版本 v3.4.13 连接到:mongodb://127.0.0.1:27017 MongoDB 服务器版本:3.4.13 我需要运行命令吗?
标签: node.js mongodb mongoose mongodb-query