如何使用项目列表更新 firestore 文档中的地图
How to update maps in a document in firestore with a list of items
我有一个列表 [a,d,f]。我想分别增加测试地图中的那些字段。
如果我对它进行硬编码,它将看起来像这样。
firestore.collection('x').doc('y').update({
'test.a':FieldValue.increment(1),
'test.d':FieldValue.increment(1),
'test.f':FieldValue.increment(1),
});
但是如果列表总是变化怎么办。如何每次都根据列表递增地图?如有任何建议,我们将不胜感激!
您可以尝试从该列表创建地图:
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)
我有一个列表 [a,d,f]。我想分别增加测试地图中的那些字段。
如果我对它进行硬编码,它将看起来像这样。
firestore.collection('x').doc('y').update({
'test.a':FieldValue.increment(1),
'test.d':FieldValue.increment(1),
'test.f':FieldValue.increment(1),
});
但是如果列表总是变化怎么办。如何每次都根据列表递增地图?如有任何建议,我们将不胜感激!
您可以尝试从该列表创建地图:
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)