Reputation: 3168
I have a document with the following structure:
{
"email" : "[email protected]",
"value" : 100,
"children" : [
{
"email" : "[email protected]",
"value" : 100
},
{
"email" : "[email protected]",
"value" : 200
}
]
}
I want to remove all elements with the email [email protected]
from the children
array. I am able to remove one item if I pass the whole object to be removed like this:
FieldValue.arrayRemove(childObject)
But I want both the objects with the email [email protected]
to be removed. Is there anyway to achieve this using FieldValue.arrayRemove()
?
Upvotes: 8
Views: 6623
Reputation: 2023
As an update, it is still the case that you must match the object exactly to remove it from an array. Additionally, of course, in the example above, he is querying for a value, which requires a query to see what matches.
However, depending on the logic: if you use a Map instead...for instance in the case above, adjusted:
"children" :
"[email protected]_100":
{
"email" : "[email protected]",
"value" : 100
},
"[email protected]_200":
{
"email" : "[email protected]",
"value" : 200
}
You can simply use:
'[email protected]_200': FieldValue.delete(),
As of late, I've gravitated away from Lists to Maps for this reason.
Upvotes: 3
Reputation: 599766
The arrayRemove
operation removes the exact item that you specify from the array. There is no way to pass a partial object and remove all array items that match the partial information. You will have to pass in each complete item that you want to remove.
If you don't know what those items are yet, you will typically have to first read the document, loop over the items in the array to remove them, and write the modified array back to the document.
Upvotes: 8