2

here is what I am trying to do:

  1. There is an array k, stored under 'posts' index under the below document:

    k=['a','b',c']
    
    firebase.firestore().collection('posts').doc(uid)
    
  2. Merge the below array to the existing array:

    k=['c','d']
    result=['a','b','c','d','e']
    

Is there a built-in syntax to do this in Firestore? I tried arrayUnion() method, like below, but it seems that it can insert a single element, not an actual array:

   firebase.firestore().collection('posts').doc(uid).update({
        posts: firebase.firestore.FieldValue.arrayUnion(k)
   })
  

Any idea on how to do this? Thanks a lot in advance!

J.Ko
  • 6,561
  • 3
  • 12
  • 28
  • `SetOptions.merge` might help in you're case add it in the update field. – CodeRED Innovations Jul 20 '20 at 10:49
  • Does this answer your question? [How to update an "array of objects" with Firestore?](https://stackoverflow.com/questions/46757614/how-to-update-an-array-of-objects-with-firestore) – Chris32 Jul 22 '20 at 13:57

2 Answers2

1

You try some like this

firebase.firestore().collection('posts').doc(uid).set({k: this.k}, {merge: true})

set with merge: true, if document or field in document exist is updated if not is created

0

There is a breaking change in the Firebase Firestore you have to use the new way to set the merge key. I am doing this in dart as follows you can try this way hope it will work for you

FirebaseFirestore.instance
        .collection('ABC')
        .doc({uid})
        .set(data, SetOptions(merge: true));
K K
  • 952
  • 9
  • 25