Mongoose delete array element in document and save

后端 未结 6 435
盖世英雄少女心
盖世英雄少女心 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:50

    The checked answer does work but officially in MongooseJS latest, you should use pull.

    doc.subdocs.push({ _id: 4815162342 }) // added
    doc.subdocs.pull({ _id: 4815162342 }) // removed
    

    https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-pull

    I was just looking that up too.

    See Daniel's answer for the correct answer. Much better.

提交回复
热议问题