如何在对象类型文档字段的属性上使用FieldValue.increment() [英] How to use FieldValue.increment() on the property of an object type document field
问题描述
Firestore的 FieldValue.increment(someValue)
在其他字段上正常工作,但在地图上无法正常工作.
Firestore's FieldValue.increment(someValue)
is working properly with other fields but not with map.
我正在尝试增加地图中属性的值.我有一个名为用户的文档.它具有地图 points:{id:123,total:100}
.我想更新地图 points
中的 total
的值.
I am trying to increment the value of a property in a map. I have a document named user. It has a map points:{id:123, total:100}
. I want to update the value of total
in map points
.
firestore.collection('users').doc('user').update({
"name": "atul",
"points": { "total": firestore.FieldValue.increment(50) }
})
而不是将 total
的值增加到150,而是将其值变成100.
Instead of incrementing the total
value to 150, its value becomes 100.
推荐答案
您需要调出字段的完整路径以递增,而不是将其隐藏在对象中.字段的完整路径涉及使用.
作为每个字段名称之间的分隔符:
You'll need to call out the full path of the field to increment, not bury it inside an object. The full path of the field involves using .
as a separator between the names of each field:
firestore.doc(...).update({
"points.total": firestore.FieldValue.increment(50)
})
这篇关于如何在对象类型文档字段的属性上使用FieldValue.increment()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!