Firestore修补程序mapValue REST API [英] Firestore Patch mapValue REST API

查看:118
本文介绍了Firestore修补程序mapValue REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用POSTMAN发送发布请求以更新一个地图条目的值,但是我无法为此找到正确的语法.这就是我将数据发布到数据库中的方式:

I am trying to send a post request using POSTMAN to update the value for one map entry, but I am unable to find the right syntax for this. This is how i posted the data in my db:

{
    "fields" : {
        "8" : { 
            "mapValue" : { 
                "fields" : {
                     "f1" :  { "stringValue" : "A" },
                     "end" :  { "integerValue" : "9" },
                }
            }
        },
        "9" : { 
            "mapValue" : { 
                "fields" : {
                     "f1" :  { "stringValue" : "A" },
                     "end" :  { "integerValue" : "10" },
                }
            }
        },
    }
}

如何将键8的值f1修改为"B"?

How can I modify the value f1 of key 8 to "B" ?

推荐答案

要更新某些值,您需要使用 PATCH .Firestore映射值是对象,可通过查询参数中的更新掩码访问,并且您还需要定义 JSON请求正文获取您要更新的资源.

For an update to certain values, you need to use PATCH. Firestore Map Values are objects, accessible through update masks in a query parameter, and you also need to define a JSON request body for the resource you want to update.

因此,对于端点,它看起来像这样:

So, for the endpoint, it would look like this:

https://firestore.googleapis.com/v1/projects/{project_id}/databases/{database_id}/documents/{document_path}?updateMask.fieldPaths=KEYVALUE.VALUE

如您所见,由于地图值是对象,因此您需要使用点符号.

As you can see, you need to use dot notation, as map values are objects.

请求正文:

{
"fields": {
    "A8": {
      "mapValue": {
        "fields": {
          "f1": {
            "stringValue": "B"
          }
        }
      }
    }
  }
}

要注意的一件事是,您的密钥只是数字.Firestore不喜欢这样,因此请将其更改为字母数字键(例如"A8"而不是"8").更改它们后,将可以访问您的值,例如 A8.f1 .

One thing to note, is that your keys are only numbers. Firestore doesn't like that, so change them for alphanumerical keys (like "A8" instead of "8"). Once you change them, access to your values, such as A8.f1 will be possible.

如果要更新多个键,请使用与号将它们添加到查询参数中,如下所示:

If you want to update for multiple keys, add them to your query parameters with ampersands, like so:

?updateMask.fieldPaths=A8.f1&updateMask.fieldPaths=A9.f1

并添加额外的JSON,以便在同一请求中进行更改;

And add the extra JSON in order to make that change inside the same request;

{
"fields": {
    "A8": {
      "mapValue": {
        "fields": {
          "f1": {
            "stringValue": "B"
          }
        }
      }
    },
    "A9": {
      "mapValue": {
        "fields": {
          "f1": {
            "stringValue": "C"
          }
        }
      }
    }
  }
}

PS:请记住使用承载令牌来发出请求.

PS: Remember to use your Bearer Token to make requests.

以下是一些地图值参考链接:

Here's some links for reference on map values:

https://firebase.google.com/docs/firestore/manage-data/data-types https://cloud.google.com/firestore/docs/reference/休息/v1/值

这篇关于Firestore修补程序mapValue REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆