Firestore: Updating field in an object removes previous field

Viewed 2710

I am using Flutter cloud firestore. Here is how my database looks like.

enter image description here

I want to add more fields (like "2222") in a_numbers object. I use updateData()like this,

DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({"a_numbers": {"2222" : false}});

The above snippet basically deletes the previous value (1111) and then updates the database with 2222 field.

Any solution?

2 Answers

To update 'a_numbers' with Pair without removing the previous values, you should assign the reference path (example: 'a_number.2222') to K and assign value to V. I edited your code. please review it for more information.

DocumentReference ref = Firestore.instance.document("products/-LMhR5cAyW4T0sa03UtU");
ref.updateData({'a_numbers.2222': value});

This is the expected behavior, in your Flutter application, you need to store a_numbers in a Map and add any new (key,value) pairs using myMap.addAll({key:value}), then you can use ref.updateData(myMap)

Related