Search code examples
google-cloud-firestorefirebase-security

What will the "request.resource.data" contain if I only update a single field of a document among several fields in firestore database?


Suppose, I have several fields in the favorites document and it looks like below:

{
favorites:
   {
      "color": "Red",
      "place": "ocean",
      "time": "morning"
   }
}

Now, I want to update a single field in this document. If I write the below code to update a single field, will the request.resource.data contain only this field or will it also contain the other fields in this document?

db.collection("users")
  .document("favorites")
  .update({
    "color": "Red"
  });

Solution

  • The request.resource variable contains the document as it'll exist after the update, if that update is allowed.

    So request.resource.data contains your updated color and the place and time values from the existing document.