Firestore Add value to array field
Solution 1
From the docs, they added a new operation to append or remove elements from arrays. Read more here: https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array
Example:
var admin = require('firebase-admin');
// ...
var washingtonRef = db.collection('cities').doc('DC');
// Atomically add a new region to the "regions" array field.
var arrUnion = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
});
// Atomically remove a region from the "regions" array field.
var arrRm = washingtonRef.update({
regions: admin.firestore.FieldValue.arrayRemove('east_coast')
});
Solution 2
Firestore currently does not allow you to update the individual fields of an array. You can, however, replace the entire contents of an array as such:
admin.firestore().doc(`users/${userA}/chats`).update('array', [...]);
Note that this might override some writes from another client. You can use transactions to lock on the document before you perform the update.
admin.firestore().runTransaction(transaction => {
return transaction.get(docRef).then(snapshot => {
const largerArray = snapshot.get('array');
largerArray.push('newfield');
transaction.update(docRef, 'array', largerArray);
});
});
Solution 3
This is 2021 and after many updates of firebase firestore, the new method to add data in array without removing another data is
var washingtonRef = db.collection("cities").doc("DC");
// Atomically add a new region to the "regions" array field.
washingtonRef.update({
regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
// Atomically remove a region from the "regions" array field.
washingtonRef.update({
regions: firebase.firestore.FieldValue.arrayRemove("east_coast")
});
Kravitz
Updated on July 09, 2022Comments
-
Kravitz almost 2 years
Im trying to use Firebase cloud functions to add the id of a chatroom to the users document in an array field. I cant seem to figure out the way to write to an array field type. here is my cloud function
exports.updateMessages = functions.firestore.document('messages/{messageId}/conversation/{msgkey}').onCreate( (event) => { console.log('function started'); const messagePayload = event.data.data(); const userA = messagePayload.userA; const userB = messagePayload.userB; return admin.firestore().doc(`users/${userA}/chats`).add({ event.params.messageId }).then( () => { }); });
here is the way my database looks
any tips greatly appreciated, Im new to firestore.
-
Brian Sternari almost 6 yearsNote that const largerArray will be converted to a number related to the array.length after the "push" operation. The proper way to do this is create a reference of the array then push, and then use that reference to update the transaction: const largerArray = snapshot.get('array'); largerArray.push('newField')
-
Juan Antonio about 5 yearsBe careful with details about FieldValue methods.
-
FirefoxMetzger almost 5 years
arrayUnion
will only add the element if it doesn't exist already. Is there a way to add items irrespective of duplicates? -
Slick Slime over 4 yearsHow to perform the above operations using a transaction?
-
Crashalot over 3 yearsThis answer is no longer correct. This feature exists. Firebase documentation: firebase.google.com/docs/firestore/manage-data/…
-
Ricky Levi almost 3 yearsrequires
update
permissions on that path -
Rohit Kumar over 2 years@FirefoxMetzger what if array contains multiple fields