Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Firestore database documents from Postman

I'm trying to create multiple documents at once in a Firestore collection with configuration purposes. I can create documents one by one sending from Postman:

{
    "fields": {
        "category":{"stringValue": "Configs"},
        "order":{"integerValue":"3"}
    }
} 

I was wondering if there is some way to create multiple documents sending something like this:

{
    "batch": [
        {
            "fields": {
                "categoria": {
                    "stringValue": "Configurações"
                },
                "ordem": {
                    "integerValue": "3"
                }
            }
        },
        {
            "fields": {
                "categoria": {
                    "stringValue": "Configurações"
                },
                "ordem": {
                    "integerValue": "3"
                }
            }
        }
    ]
}

Can anyone say if is it possible? If so, how can I do that?

like image 714
Nowdeen Avatar asked May 18 '26 02:05

Nowdeen


1 Answers

Something like the below should work.

Although it is an "update" action is creates new documents as well if they don't exist. The document ID must be provided, it cannot be auto generated using the commit endpoint.

I was able to get my use case to work after viewing this response detailing use of commit.

API Endpoint: https://firestore.googleapis.com/v1/projects/projectID/databases/(default)/documents:commit

{
    "writes": [
        {
            "update": {
                "name": "projects/projectID/databases/(default)/documents/test/ExistingDocumentID1",
                "fields": {
                    "comment": {
                        "stringValue": "Hello World!"
                    }
                }
            }
        },
        {
            "update": {
                "name": "projects/projectID/databases/(default)/documents/test/NewManualDocumentID2",
                "fields": {
                    "comment": {
                        "stringValue": "Happy Birthday!"
                    }
                }
            }
        }
    ]
}
like image 181
Gerry St.Pierre Avatar answered May 20 '26 19:05

Gerry St.Pierre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!