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!

2

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

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.