2

I have a situation where i want to remove a field inside a array of dictionary.

I know how to remove a normal field: { $unset: { : ""} }

But please help me for below example where i want to remove status field inside second dict

{  
    "recs":[  
        {  
            "required_1":""
            "required_2":"info",
            "status":"completed",
            "datetime":"2018-02-02T06:00:04Z"
        },
        {  
            "required_1":""
            "required_2":"info",
            "status":"inprogress"
            "datetime":"2018-02-02T06:00:04Z"         
        }
    ],
    "mainstatus":"failed"
}
Caconde
  • 4,177
  • 7
  • 35
  • 32
Avinash Hegde
  • 41
  • 1
  • 3

2 Answers2

0

You have to use the $ sign : recs.$.status will select the status field of all records in recs.

https://docs.mongodb.com/manual/reference/operator/update/positional/

HRK44
  • 2,382
  • 2
  • 13
  • 30
-1

You have to use $pull to remove data from the array.

db.getCollection('TEST').update({/* some condition */}, {
    $pull: {
        recs: {"status" : "inprogress"}
    }
});
Rahul Sharma
  • 9,534
  • 1
  • 15
  • 37