0

I have a huge number of records in the collection on the below structure.

Here I want to update all floor fields as an empty string "", wherever it's "n/a". It should not affect other blocks which already have value for the floor like the First, Second Floor.

Can someone help on this?

{
    "id" : "181",
    "EID" : "83",
    "History" : [ 
        {
            "aNum" : "12324",
            "dev" : [ 
                {
                    "type" : "",
                    "room" : "Office",
                    "floor" : "Second Floor"
                }, 
                {
                    "type" : "",
                    "room" : "Bedroom",
                    "floor" : "n/a"
                },
                {
                    "type" : "",
                    "room" : "Bedroom",
                    "floor" : "First Floor"
                },
                {
                    "type" : "",
                    "room" : "Bedroom",
                    "floor" : "n/a"
                },
            ]
        }
    ]
}

1 Answer 1

2

With arrayFilters and filtered $[<identifier>] operator.

db.collection.update({},
{
  $set: {
    "History.$[].dev.$[dev].floor": ""
  }
},
{
  arrayFilters: [
    {
      "dev.floor": "n/a"
    }
  ],
  multi: true
})

Sample Mongo Playground

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.