查找文档,然后在数组中查找条目并将其从数组中删除

Find document, then find entry within array and remove it from the array

在 Node 和 Mongoose 中,我想从文档中的数组中删除一个对象。结构是这样的

{ 
    _id: ObjectId,
    title: String,
    tags: [
        { text: String },
        { text: String }
    ]
}

我将按 _id 查找项目,但随后我想在标签内查找某个 String 并将其从数组中删除。

您实际上想要 update the document and use the $pull 运算符与查询匹配 "tags.tag":

下的匹配值
Model.update(
    { "_id": docId, "tags.tag": "mytag" },
    { "$pull": { "tags": { "tag": "mytag" } },
    function(err,numAffected) {

    }
)