简体   繁体   English

如何手动设置保存调用 Firestore API 的新文档的 Firestore 文档 ID?

[英]How to manually set the Firestore document ID saving a new document calling Firestore API?

I am finding the following problem trying to save a new document into Firesotore database calling the related POST API and manually setting the document ID.我发现以下问题试图将新文档保存到 Firesotore 数据库中,调用相关的 POST API 并手动设置文档 ID。

I am using Python but I suppose that the problem is not related to the language.我正在使用 Python 但我认为问题与语言无关。

I try to explain what I have done and what is not working.我试着解释我做了什么,什么没有用。 My first attempt (that works but automatically set the document ID on Firestore) was:我的第一次尝试(有效但自动在 Firestore 上设置文档 ID)是:

First of all I created this JSON document that will be the payload of my API:首先,我创建了这个 JSON 文档,它将成为我的 API 的有效载荷:

# Convert the record to a dictionary
doc = {
    'fields': {
        'surname': {'stringValue':record[2]},
        'firstName': {'stringValue':record[1]},
        'socialSecurityCode': {'stringValue':codici_fiscali_list_as_string},
        'city': {'stringValue':record[4]},
        'personalPhone': {'stringValue':record[5]},
        'personalPhone2': {'stringValue':record[6]},
        'personalEmail': {'stringValue':emails_list_as_string},
        'pazPres': {'stringValue':record[7]},
        'pazNotes': {'stringValue':record[8]},
        'pazMemo': {'stringValue':record[9]},
        'isArchived': {'booleanValue':isArchived},
        'isMigrated': {'booleanValue':True},
        #'decomposition_keyword_search_list':{'arrayValue':{'values':decomposition_keyword_search_list}}
        "decomposition_keyword_search_list":{
         "arrayValue":{
            "values":[
            ]
         }
      }
    }    
}

then I perform the API call by these lines:然后我通过这些行执行 API 调用:

api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'

response = requests.post(api_endpoint, json=doc)

It works fine and it put the expected document into my test collection.它工作正常,并将预期的文档放入我的测试集合中。 But in this way the ID was automatically generated by Firestore.但是通过这种方式,ID 是由 Firestore 自动生成的。 For some reason I have to use the content of a variable as ID (my ID must be the value of my record[0] that is an unique string)出于某种原因,我必须使用变量的内容作为 ID(我的 ID 必须是我的记录 [0]的值,这是一个唯一的字符串)

So I tried to change the previous API endpoint in the following way:所以我尝试通过以下方式更改之前的 API 端点:

api_endpoint = 'https://firestore.googleapis.com/v1/projects/MY-PROJECT-NAME/databases/(default)/documents/test/'+ record[0]

I expected that it creates document using the record[0] as document ID but it seems that I am wrong since I am obtaining the following error message:我预计它会使用record[0]作为文档 ID 创建文档,但似乎我错了,因为我收到以下错误消息:

Error saving document: {
  "error": {
    "code": 400,
    "message": "Document parent name \"projects/MY-PROJECT-NAME/databases/(default)/documents/test\" lacks \"/\" at index 71.",
    "status": "INVALID_ARGUMENT"
  }
}

So, what is wrong?那么,有什么问题吗? What am I missing?我错过了什么? How can correctly manually set the ID of the document that I am creating calling the previous API?如何正确手动设置我正在创建的文档的 ID 调用以前的 API?

Take a look at the documentation for creating documents .查看创建文档的文档 If you want to specify a document ID, it says you should pass that as a query parameter called documentId :如果你想指定一个文档 ID,它说你应该将它作为一个名为documentId查询参数传递:

The client-assigned document ID to use for this document.用于此文档的客户端分配的文档 ID。

Optional.选修的。 If not specified, an ID will be assigned by the service.如果未指定,则服务将分配一个 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM