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 ?
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").
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.
// 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With