0

I have a a map within a firestore document that looks like this:

enter image description here

Right now I am confused on how to:

  1. Update the value of the one item in the map
  2. Add a new value to the map

I thought Update() would be my best option based on the documentation:

Set() in firestore:

To create or overwrite a single document, use the set() method:

Update() in firestore:

To update some fields of a document without overwriting the entire document, use the update() method:

So I want to update just my inPack map. I am doing that with this code:

                    db
                        .collection('GearLockerItems')
                        .doc(doc.reference.id)
                        .update(
                      {
                        'inPack': {packID: false},
                      },
                    );

My issue is that it is overwriting the valueId and not adding a new valueID. So I am updating the inPack by overwriting the entire thing. I think I am not getting down to the next level and am stuck.

Ten Digit Grid
  • 1,315
  • 4
  • 22
  • 43

1 Answers1

3

Use dot notation as follows:

db
    .collection('GearLockerItems')
    .doc(doc.reference.id)
    .update({'inPack.$packId': false},
);

From the official documentation:

Dot notation allows you to update a single nested field without overwriting other nested field. If you update a nested field without dot notation, you will overwrite the entire map field

Robert Sandberg
  • 6,832
  • 2
  • 12
  • 30