Add data to specific document (FIREBASE)

Hi there guys,

I got a question about the REST API firebase service.

Im trying to “ADD” data to existing document, following the next URL to make the HTTP Request, but it doesn’t create new fields, just replace the existing ones.

https://firestore.googleapis.com/v1beta1/projects/{projectId}/databases/{databaseId}/documents/{document_path}?updateMask.fieldPaths={fieldName}&updateMask.fieldPaths={fieldName2}

Somebody tried to do this before?, it’s possible?, I following this topic trying to solve this through this specific method but it doesn’t work.

Updating single fields and other random Firebase REST API tips - Community guide - AppGyver

This is an example of what exist now in my DB:

[
    {
        "name": "projects/xxxxx/databases/(default)/documents/commune_fees/country",
        "fields": {
            "commune_price": {
                "integerValue": "55555"
            },
            "commune_name": {
                "stringValue": "caracas"
            }
        },
        "createTime": "2022-03-10T17:41:50.831919Z",
        "updateTime": "2022-03-10T18:17:55.784386Z"
    }
]

And this is how I want to update it:

[
    {
        "name": "projects/xxxxx/databases/(default)/documents/commune_fees/country",
        "fields": {
            "commune_price": {
                "integerValue": "55555"
            },
            "commune_name": {
                "stringValue": "caracas"
            }
        },
        "createTime": "2022-03-10T17:41:50.831919Z",
        "updateTime": "2022-03-10T18:17:55.784386Z"
    }
    {
        "name": "projects/xxxxx/databases/(default)/documents/commune_fees/country",
        "fields": {
            "commune_price": {
                "integerValue": "88888"
            },
            "commune_name": {
                "stringValue": "Japan"
            }
        },
        "createTime": "2022-03-10T17:41:50.831919Z",
        "updateTime": "2022-03-10T18:17:55.784386Z"
    }
]

Thanks in advance. :slight_smile:

Hi! Looking at your example, it does not look like you are trying to update a single Object, but that you’re trying to add another one. In that case, use Create :slight_smile:

Hi Mevi, thanks for your time to reply.

I just used this to add data inside a single document, just in case somebody need to do something similar.

How to create Subcollection with a Document inside of it using POST method for REST

Use the following URL. In the body of the HTTP request, write the intended field of the document.

Now it looks like this (as I wanted):

1 Like