【问题标题】:Update Nested Field in Cloud Firestore Using Dynamic Key and Auto Increment Swift使用动态键和自动增量 Swift 更新 Cloud Firestore 中的嵌套字段
【发布时间】:2021-01-23 00:00:01
【问题描述】:

以 Firestore 文档为例,你可以看到他们有

{
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", height(cm): "100" },
    age: 12
}

// To update favorite color:
db.collection("users").doc("frank").update({
    "favorites.color": "Red"
})

现在,如果我想自动增加年龄,使用文档也很容易,我可以看到并测试以下内容是否有效:

db.collection("users").doc("frank").updateData([
    "age": Fieldvalue.increment(Int64(1))])

我想知道是否有办法使用动态键来增加高度。所以想做类似的事情:

let updateKey = "favourites.height(cm)" 
db.collection("users").doc("frank").updateData([
    updateKey: FieldValue.increment(Int64(10))])


//which results in
{
    name: "Frank",
    favorites: { food: "Pizza", color: "Blue", height(cm): "110" },
    age: 12
}

我怎样才能做到这一点?

【问题讨论】:

    标签: swift dynamic google-cloud-firestore nested google-cloud-functions


    【解决方案1】:

    Firestore increment 不适用于字符串 favourites.height(cm)。您需要将此字段更改为数字,然后使用增量功能。否则它只会将此字段设置为增量方法指定的值

    【讨论】:

      猜你喜欢
      • 2018-04-27
      • 2018-11-12
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-02
      • 1970-01-01
      相关资源
      最近更新 更多