Reputation: 1306
I am building a Google Docs Extension using GAS which uses Firebase's Cloud Firestore.
I have a collection named GROUPS
, under which I have a test document called Test
, which has these fields:
{
DISPLAY_IDS: {
TYPE1: [array_of_numbers],
TYPE2: [array_of_numbers],
TYPE3: [array_of_numbers],
}
}
I need to update a specific TYPE
every time (the whole array at each time, no need to be able to push an element or something like that).
I have seen that in order to do this using the regular JS API you can use the dot notation, and do it like this:
firestore.collection("GROUPS").document("Test").update({
"DISPLAY_IDS.TYPE2": new_array_of_numbers,
});
So using the REST API I have assumed it would have been translated into:
// patch request
payload: {
fields: {
"DISPLAY_IDS.TYPE2": new_array_of_numbers
}
}
url: "https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/GROUPS/Test?updateMask.fieldPaths=DISPLAY_IDS.TYPE2"
This however did not work, and instead it created a new field called DISPLAY_IDS.TYPE2
outside of DISPLAY_IDS
.
I have also tried the following request after looking a bit in the internet:
// patch request
payload: {
fields: {
DISPLAY_IDS: {
TYPE2: new_array_of_numbers,
}
}
}
url: "https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/GROUPS/Test?updateMask.fieldPaths=DISPLAY_IDS"
But here what happened was that DISPLAY_IDS
was entirely overridden, the other types were deleted.
I have also tried a combination of the above, so the payload was without the dot notation, and the mask was with the dot notation, but this did not update the database at all (which I assume makes sense as the documentation states that if a field appears in the mask but not in the payload then it would be deleted (but the field DISPLAY_IDS.TYPE2
(not the nested one, just this name) already doesn't exist) and that fields which are in the payload but not in the mask would be ignored).
What is the correct way to only update the TYPE2
nested field using the REST API?
Upvotes: 0
Views: 329
Reputation: 50840
You can specify a mapValue
as shown below:
{
"fields": {
"DISPLAY_IDS": {
"mapValue": {
"fields": {
"TYPE2": {
"stringValue": "UPDATED VALUE"
}
}
}
}
}
}
You must specify DISPLAY_IDS.TYPE2
in updateMask.fieldPaths
query params otherwise others fields in the document will be removed. Just append this to your API URL to do so:
?updateMask.fieldPaths=DISPLAY_IDS.TYPE2
Upvotes: 1