Mongoose delete array element in document and save

后端 未结 6 438
盖世英雄少女心
盖世英雄少女心 2020-11-28 23:53

I have an array in my model document. I would like to delete elements in that array based on a key I provide and then update MongoDB. Is this possible?

Here\'s my

6条回答
  •  甜味超标
    2020-11-29 00:35

    Answers above are shown how to remove an array and here is how to pull an object from an array.

    Reference: https://docs.mongodb.com/manual/reference/operator/update/pull/

    db.survey.update( // select your doc in moongo
        { }, // your query, usually match by _id
        { $pull: { results: { $elemMatch: { score: 8 , item: "B" } } } }, // item(s) to match from array you want to pull/remove
        { multi: true } // set this to true if you want to remove multiple elements.
    )
    

提交回复
热议问题