0
Schema images: [Objects]

document has been registered in mongoose is

"images" : [ 
    {
        "id" : "1212",
        "name" : "image1"
    }, 
    {
        "id" : "1222",
        "name" : "image2"
    }, 
    {
        "id" : "1234",
        "name" : "image3"
    }, 
    {
        "id" : "123",
        "name" : "image4"
],

So what is query I should pass arrays of IDs to remove array of objects that are subdocuments. I tried using $pullall but couldn't achieve.

Vivek S
  • 115
  • 1
  • 12
  • can you show your tried query and what you are getting in result. – turivishal Jun 04 '21 at 07:10
  • await User.findByIdAndUpdate(rid, { $pullAll: { images: imagesIds } }, { new: true }) this is my query and I get no errors but also no update in document. @turivishal – Vivek S Jun 04 '21 at 10:39
  • use $pull, and you implementation is not correct, check [documentation](https://docs.mongodb.com/manual/reference/operator/update/pull/), try `{ $pull: { "images": { id: { $in: imagesIds } } } }` – turivishal Jun 04 '21 at 10:50
  • Does this answer your question? [MongoDB - Pull multiple objects from an array](https://stackoverflow.com/questions/48709923/mongodb-pull-multiple-objects-from-an-array) – turivishal Jun 04 '21 at 10:51
  • Thank you @turivishal, { $pull: { images: { id: { $in: imagesIds } } } }, this really worked for me. Thank you so much! – Vivek S Jun 04 '21 at 12:47

0 Answers0