使用 flutter,当更新具有 2 个或多个连续相同值的数组时,firestore 只会添加一个。
示例:
void updateValue() {
var now = [new DateTime.now()];
int pain =5;
Firestore.instance.collection('Patient').document('bC2ML7LaQ1fWGOl37ZUq').collection('Symptom').document('2H6e99Bvrz6h0HBzgnyg')
.updateData({ 'nauseaLevel' : FieldValue.arrayUnion([pain]), 'nauseaTime':FieldValue.arrayUnion(now)});
}
当执行该函数2次时,firestore中的数组“nauseaLevel”只会添加一个“pain”值而忽略第二个。
数组 nauseaTime 按预期工作,因为值不同。
最佳答案
根据有关 updating elements in an array 的官方文档:
arrayUnion()
adds elements to an array but only elements not already present.
因此,您无法使用 arrayUnion()
函数在 Cloud Firestore 的数组中添加重复元素。
然而,为了避免误解,您可以在数组中添加重复元素,但前提是您读取整个数组客户端,进行添加(包括所有重复项),然后将其写回数据库。
关于arrays - Firestore 在更新数组时不存储两个相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56110491/