64

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.

1

4 Answers 4

138

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')
});
Sign up to request clarification or add additional context in comments.

5 Comments

Be careful with details about FieldValue methods.
arrayUnion will only add the element if it doesn't exist already. Is there a way to add items irrespective of duplicates?
How to perform the above operations using a transaction?
requires update permissions on that path
@FirefoxMetzger what if array contains multiple fields
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);
  });
});

2 Comments

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')
This answer is no longer correct. This feature exists. Firebase documentation: firebase.google.com/docs/firestore/manage-data/…
11

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")
      });

Comments

1

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')});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.