简体   繁体   中英

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

In Node and Mongoose, I'd like to remove an object from an array in a document. The structure is like this.

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

I will look up an item by it's _id , but then I want to look within the tags for a certain String and remove that from the array.

You effectively want to update the document and use the $pull operator with a query matching the matching value under "tags.tag" :

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

    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM