59

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

enter image description here

any tips greatly appreciated, Im new to firestore.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Kravitz
  • 2,769
  • 6
  • 26
  • 53

4 Answers4

132

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')
});
Neil
  • 7,482
  • 6
  • 50
  • 56
Sean Blahovici
  • 5,350
  • 4
  • 28
  • 38
11

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);
  });
});
  • 1
    Note 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') – Brian Sternari May 15 '18 at 19:20
  • 9
    This answer is no longer correct. This feature exists. Firebase documentation: https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array – Crashalot Sep 23 '20 at 03:43
9

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")
      });
Swaroop Maddu
  • 4,289
  • 2
  • 26
  • 38
Neeta Chaurasia
  • 101
  • 1
  • 1
0

With firebase 9.x:

import { getFirestore, FieldValue } from 'firebase-admin/firestore';
import { initializeApp } from 'firebase-admin/app';
import admin from "firebase-admin";

const firebaseAdminApp = initializeApp ({
   credential: admin.credential.cert(serviceAccountCreds) 
});

const db = getFirestore(firebaseAdminApp);

let collectionName = 'cities';
let docID = 'DC'

let docRef = await db.collection(collectionName).doc(docID);
await docRef.update({regions: FieldValue.arrayUnion('Northern Virginia')});
SV125
  • 273
  • 1
  • 3
  • 13