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 = {joe@gmail.com, dan@gmail.com}
, and I want to append newEmails = {mat@gmail.com, sharon@gmail.com}
to it.
Problem
The only options I know for this are:
Reading the
friendEmails
array first, then adding the union of it andnewEmails
to the document.Iterating over the elements of
newEmails
(let's and each iteration doing:myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(SinceFieldValue.arrayUnion
only 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("mat@gmail.com", "sharon@gmail.com");
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?