Reputation: 63560
I have a collection containing documents of the following form:
{
tokens: {
name: {
value: "...",
},
...
}
}
name
can be anything, and there can be multiple embedded documents assigned to different keys, all of which have a value
field.
How do I $unset
all embedded documents that have a certain value
? { $unset: { "tokens.value": "test" } }
doesn't work, and nor does "tokens.$[].value"
or "tokens.$.value"
.
Upvotes: 0
Views: 95
Reputation: 9284
You can use pipelined form of update, like this:
db.collection.update({},
[
{
"$set": {
"tokens": {
$arrayToObject: {
"$filter": {
"input": {
"$objectToArray": "$$ROOT.tokens"
},
"as": "item",
"cond": {
"$ne": [
"$$item.v.value",
"test"
]
}
}
}
}
}
}
],
{
multi: true
})
In this query, we do the following:
tokens
object to an array, using $objectToArray
.value
key present in embedded documents and have a value of test
using $filter
.tokens
key.Upvotes: 1