如何在对象类型文档字段的属性上使用 FieldValue.increment()



Firestore的FieldValue.increment(someValue)与其他字段一起工作,但不能与map一起使用。

我正在尝试增加地图中属性的值。我有一个名为 user 的文档。它有一个地图points:{id:123, total:100}。我想更新地图pointstotal的值。

firestore.collection('users').doc('user').update({
    "name": "atul",
    "points": { "total": firestore.FieldValue.increment(50) }
})

其值变为 100,而不是将total值增加到 150。

您需要调用要递增的字段的完整路径,而不是将其埋在对象中。 字段的完整路径涉及使用 . 作为每个字段名称之间的分隔符:

firestore.doc(...).update({
    "points.total": firestore.FieldValue.increment(50)
})

最新更新