Firestore update nested objects

I’m trying to add a message object in a nested map. As you can see in the screenshot below, I want to go in “us” then “0” then “messages” to add my object.
enter image description here

I have used the following script :

firestore.collection(“salon”).document(idSalon).update(
“us.0.messages”,message
);

When I use it, it just overwrites everything as you can see.
enter image description here

Can someone help me, please?

Answer

As I see in your screenshot, the “us” field is of type array. To be able to update an array, you should use arrayUnion and not a simple field update. For that I recommend you check the official documentation regarding update elements in a Firestore array.

I have also written an article called:

That I think it will help you understand this topic better.