scott lee
scott lee

Reputation: 656

How to update maps in a document in firestore with a list of items

enter image description here

I have a list [a,d,f]. I want to increment those fields in the test map respectively.

If I were to hard code it, it'll look something like this.

firestore.collection('x').doc('y').update({
        'test.a':FieldValue.increment(1),
        'test.d':FieldValue.increment(1),
        'test.f':FieldValue.increment(1),
      }); 

But what if the list always changes. How can increment the map according to the list every time? Any suggestion would be appreciated!

Upvotes: 0

Views: 272

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

You can try creating a map from that list:

var myList = ["a", "d"];
var myMap = { for (var item in myList) 'test.$item': FieldValue.increment(1) };
print(myMap);
// {test.a: FieldValue.increment(1), test.d: FieldValue.increment(1)}

firestore.collection('x').doc('y').update(myMap)

Upvotes: 2

Related Questions