0

I have a document with following structure. I want to insert a new file in the array files followed by making all other file status set to "INACTIVE". After inserting, I am trying following code to update other file of the array but it is unable to match.

{
  {
 "id":"fafsahjaf867rwhrbjw",
 "files" : [
            {"name": "123", status:"ACTIVE"},
            {"name": "124", status:"ACTIVE"},
            {"name": "125", status:"ACTIVE"},
            {"name": "126", status:"ACTIVE"},
           ] 
   },
}

    query = bson.M{ "id": productId,
    "files": bson.M{
        "$not": bson.M{"$elemMatch": fileObj}}}

    update = bson.M{
    "$set": bson.M{
        "files.$.status": "INACTIVE",
    },
  }
Forkmohit
  • 733
  • 3
  • 12
  • 31

1 Answers1

0

If you have Mongo >= 3.6, here an example to edit all but the last with name 126.

db.collection.update(
  { "id" : "fafsahjaf867rwhrbjw", "files.name": "126" },
  { "$set": { "files.$[elem].status": "INACTIVE" } },
  { "arrayFilters": [{ "elem.name": {$ne : "126"} }], "multi": true }
)

Got that solution from How to Update Multiple Array Elements in mongodb

RichieK
  • 474
  • 6
  • 15