Home > Back-end >  Failing to Update Array in Firestore
Failing to Update Array in Firestore

Time:05-02

I am trying to update Array in Firestore. From what I know you cant just append to the array in Firestore instead you need to replace the complete array with a new one.

Following is the code in Kotlin where I was trying to update the array:

 holder.button.setOnClickListener {

         pending.add(o[0])
        Firebase.firestore.collection("profiles").document("YHMauLouORRtrYBV2h4dHJ5A0s72").update(

            "Pending" , "$pending"

        )

Here o[0] is the string I desire to Append to the already Existing Array named pending

Initialy in Firestore :-

enter image description here

Output I am getting :-

enter image description here

Output I desire:-

enter image description here

Basically While updating its converting the Array to a sting of list and pushing to Firestore. How can I get the Desired Output as shown in image ?

Thanks in Advance

CodePudding user response:

That's not how to update an array in Firestore. To achieve that, you should use:

Firebase.firestore.collection("profiles")
    .document("YHMauLouORRtrYBV2h4dHJ5A0s72")
    .update("Pending", FieldValue.arrayUnion("Banana"))

When you're calling update without the second argument it means that you want to update a regular field.

  • Related