8

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()?

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is this feature likely to be supported in the future?
Nobody is actively working on adding partial array operations at the moment. If you'd like to cast your vote for them to be added, you can file a feature request. But for now, you will have to query-then-remove.
3

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.

3 Comments

Hello, I want to change this '[email protected]_200' with 'children.${mail}', but not sure for the right syntax, can you help me with that?
String var = '[email protected]_200'; 'children.$var' should do it.
Unfortunately thats not working :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.