Create Firestore database documents from Postman

十年热恋 提交于 2021-01-06 05:36:11

问题


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?


回答1:


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!"
                    }
                }
            }
        }
    ]
}


来源:https://stackoverflow.com/questions/58308396/create-firestore-database-documents-from-postman

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!