问题
I have a document with the following structure:
{
"email" : "a@gmail.com",
"value" : 100,
"children" : [
{
"email" : "b@gmail.com",
"value" : 100
},
{
"email" : "b@gmail.com",
"value" : 200
}
]
}
I want to remove all elements with the email b@gmail.com
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 b@gmail.com
to be removed. Is there anyway to achieve this using FieldValue.arrayRemove()
?
回答1:
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.
来源:https://stackoverflow.com/questions/59694940/fieldvalue-arrayremove-to-remove-an-object-from-array-of-objects-based-on-prop