Firestore Update a document field Using Rest API

Here is another example giving this json structure from firestore

"fields": {
    "eth0": {
      "mapValue": {
        "fields": {
          "address": {
            "stringValue": "172.0.0.1"
          },
          "port": {
            "stringValue": "8080"
          },
          "endpoint": {
            "stringValue": "10.0.5.24"
          }
        }
      }
    }
  }

Then to update the endpoint field only

curl -sSLX PATCH \
  -H "Authorization: Bearer {TOKEN}" \
  -H "Content-type: application/json" \
  -d "{
    \"fields\": {
      \"eth0\": {
        \"mapValue\": {
          \"fields\": {
            \"endpoint\": {
              \"stringValue\": \"10.10.2.24\"
            }
          }
        }
      }
    }
  }" \
  "https://firestore.googleapis.com/v1/projects/{project-id}/databases/(default)/documents/{collection}/{document}?updateMask.fieldPaths=eth0.endpoint")

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.

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

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.


Your request body is ok. But you need to use the update mask.

From reading the documents I found that the DocumentMask is used to restrict a get or update operation on a document to a subset of its fields. So by adding 'name' to your field paths on the mask, it will only allow you to update that specific field and the others won't get deleted.

You can read more about it here.