Firestore Patch mapValue REST API

泪湿孤枕 提交于 2020-12-15 06:22:40

问题


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" },
                }
            }
        },
    }
}

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


回答1:


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.

The request body:

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

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

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: 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/rest/v1/Value




回答2:


You can update a document partially by an HTTP PATCH request to Google Cloud Firestore's REST APIs.

In this case you need to provide the following information in the request:

1) document.name as Path Parameter : The resource name of the document, for example projects/{project_id}/databases/{database_id}/documents/{document_path}

2) updateMask as Query Parameter: The fields to update.

3) Request body : JSON representation of how you want the document to look like, after the update operation.

API Reference: https://firebase.google.com/docs/firestore/reference/rest/v1/projects.databases.documents/patch

This is similar to set() operation in the client libraries where you can pass a SetOption which can be set to merge to merge the updates to an existing document.

Hope this helps!



来源:https://stackoverflow.com/questions/58702227/firestore-patch-mapvalue-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!