Firestore使用Rest API更新文档字段 [英] Firestore Update a document field Using Rest API

查看:207
本文介绍了Firestore使用Rest API更新文档字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用REST API在云Firestore中执行PATCH Opeartion.

Im trying to perform PATCH Opeartion in cloud firestore using REST API.

这是我的请求正文

`{
  "fields": {
    "name": {
      "stringValue":"Dinesh"
    }
  }
}`

当我发出请求时,文档内的所有现有字段都将被删除,只有名称字段将被更新.他们在文档中提供了文档掩码文档掩码.但是我不明白它是如何工作的,也都无法为此找到任何样本.有人知道如何只更新文档中的一个字段而不影响其他字段吗?

When i fire the request , All the existing fields inside the document are getting deleted and only the name field is getting updated. In the Documentation they have given the Document Mask Document Mask. but i dont understand how it works , neither im able to find any samples for that. Somebody know how to update only one field inside the document without affecting other fields ?

推荐答案

在没有DocumentMask对象的情况下,修补程序方法默认为用请求正文替换Firestore Document,而不是更新提交的字段并保留省略的字段.

Without a DocumentMask object, the patch method defaults to replacing the Firestore Document with the request body rather than updating the submitted fields and retaining omitted fields.

DocumentMask作为updateMask参数提交,其中包含要修补的fieldPaths.花费了一段时间,但感谢这个答案,并且我做了很多尝试,发现updateMask对象的每个fieldPath属性都需要分别单独包含在请求网址的查询字符串中:

The DocumentMask is submitted as an updateMask parameter containing the fieldPaths to be patched. It took a while but thanks to this answer and a lot of attempts I figured out that each fieldPath property of the updateMask object needs to be individually included in the query string of the request url:

https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/{document_path}?updateMask.fieldPaths=status&updateMask.fieldPaths=title

其中statustitle是请求正文中的两个字段.请注意,如果查询字符串中省略了请求正文中包含的字段,则这些字段将保持不变.

Where status and title are two fields in the request body. Note that fields included in the request body are disregarded if they are omitted from the query string, remaining unchanged.

这篇关于Firestore使用Rest API更新文档字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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