如何在Firebase集合中为文档标题设置实时侦听器-Python

时间:2019-06-19 21:19:55

标签: python firebase google-cloud-firestore

我正在尝试听Firebase集合进行文档创建。

firebase文档的布局如下:

# Create a callback on_snapshot function to capture changes
def on_snapshot(col_snapshot, changes, read_time):
    print(u'Callback received query snapshot.')
    print(u'Current cities in California:')
    for doc in col_snapshot:
        print(u'{}'.format(doc.id))

col_query = db.collection(u'cities').where(u'state', u'==', u'CA')

# Watch the collection query
query_watch = col_query.on_snapshot(on_snapshot)

代替where()内的“状态”,有什么方法可以引用文档标题? (文档中没有标题)

我尝试使用u'__name__',但收到错误消息:

RuntimeError: Error 3: a filter on __name__ must be a document resource name

1 个答案:

答案 0 :(得分:0)

JavaScript SDK似乎有一个明确的选项,如here所示:

db.collection('books').where(firebase.firestore.FieldPath.documentId(), '==', 'fK3ddutEpD2qQqRMXNW5').get()

因此firebase.firestore.FieldPath.documentId()表示文档ID。

Python中没有直接等效项,但是您可以通过查询__name__(该字段的内部名称)来完成相同操作:

col_query = db.collection(u'cities').where(u'__name__', u'==', u'your_document_id')
相关问题