Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {[email protected], [email protected]}, and I want to append newEmails = {[email protected], [email protected]} to it.
Problem
The only options I know for this are:
Reading the
friendEmailsarray first, then adding the union of it andnewEmailsto the document.Iterating over the elements of
newEmails(let's and each iteration doing:myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(SinceFieldValue.arrayUniononly lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("[email protected]", "[email protected]");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?