Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore Update a document field Using Rest API

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

This is my Request body

`{
  "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 ?

like image 924
Dinesh Thiyagarajan Avatar asked Mar 23 '18 10:03

Dinesh Thiyagarajan


People also ask

How do I update a field in firestore?

For updating some fields of a document without overwriting the entire document, use the update() method. This method is used in the following way: val lucknowRef=db. collection("states").

How do I use REST API on firestore?

Making REST callsAll REST API endpoints exist under the base URL https://firestore.googleapis.com/v1/ . To create a path to a document with the ID LA in the collection cities under the project YOUR_PROJECT_ID you would use the following structure. To interact with this path, combine it with the base API URL.

How do you update an array in firestore?

// Atomically remove a region from the "regions" array field. # Atomically add a new region to the 'regions' array field. # // Atomically remove a region from the 'regions' array field. # Atomically add a new region to the 'regions' array field.

How do I add a field to an existing document in firestore?

Build a DocumentReference to the document you want to update, then use the update() method on the DocumentReference to indicate only the fields to be added or changed. Pass it an object with only properties that match the fields to add or change.


2 Answers

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.

like image 189
remyActual Avatar answered Sep 22 '22 22:09

remyActual


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.

like image 25
Nutdesigns Avatar answered Sep 25 '22 22:09

Nutdesigns