In Firestore, I have a document containing a map called myMap, which contains an array field called myArr.
Is it possible to use arrayUnion to update myArr, by adding an element to it, within a transaction?
await admin.firestore().runTransaction(async (transaction: Transaction) => {
//
const docRef: DocumentReference = admin.firestore()
.doc(`collection/document`);
const doc: DocumentSnapshot = await transaction.get(docRef);
if (doc.exists)
await transaction.update(docRef,
{'myMap': {'myArr': FieldValue.arrayUnion('myNewValue')},}
);
});
I believe the above code should add myArr, but it's instead replacing the entire array.
What am I doing wrong?