简体   繁体   中英

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

在此处输入图像描述

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!

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM