I am using mongo "$unset" command to remove all matching documents for a certain criteria where index is not known.
Let's say collection looks like:-
{
"_id" : 1,
"list" : [
{
"key" : "a"
},
{
"key" : "b"
},
{
"key" : "c"
}
]
}
Mongo shell command used to unset keys matching "b":-
db.test.update({"list.key":"b"}, {$unset: {'list.$.key':"b"}})
Result:-
{
"_id" : 1,
"list" : [ {"key" : "a"}, {}, {"key" : "c"} ]
}
Answer needed for:- How to remove the empty array object?
Note:- I have read pages suggesting to use $pull:null, but that doesn't fit in here.
Thanks!