首页 > 解决方案 > 在 Firestore 中创建时指定数字字段值?

问题描述

我想在创建时在 firestore 数据库中创建一个整数值。但是,我传递的每个变量都会变成一个字符串。

这就是我所拥有的:

next_group_id = self.GetNextGroupId() #returns int value
try:
# get the document
    doc_ref = self._db.collection(GROUPS).document(next_group_id)      

    # create a new document  
    # this gets created as a string in firestore    
    doc_ref.set({
        u'Group Id': next_group_id, 
    })
    print("Created Group '{}'.".format(group._group_name))
except:
    print("Possible firestore error has accord - unable to create group")

标签: python-3.xfirebasegoogle-cloud-firestorefirebase-admin

解决方案


原来你不能用这样的整数搜索文档:

doc_ref = self._db.collection(GROUPS).document(next_group_id) 

所以我只是转换成一个字符串,它工作正常......

doc_ref = self._db.collection(GROUPS).document(str(next_group_id))   

推荐阅读