【问题标题】:How to tell which document was just added in firestore?如何判断 Firestore 中刚刚添加了哪个文档?
【发布时间】:2021-01-12 12:09:30
【问题描述】:

在 python 中,我正在查看 firestore 集合的更改,并且对于每个更改,我都会收到一个包含整个集合的快照。根据documentation,这不是问题,但是,我的问题是该快照中的每个文档都有一个“添加”的更改类型,那么我应该如何确定刚刚添加的是哪个文档?

#callback Function
def on_snapshot(doc_snapshot, changes, read_time):
    print('on_snapshot..')
    for change in changes:
        if change.type.name == 'ADDED':
            #this is the new document.. 
            #which of the documents in the snapshot are of change.type.name = 'ADDED'?
        else:
            #some modified/deleted document.. 

【问题讨论】:

  • 触发事件的文档将在添加案例中的快照侦听器中。
  • @s_o_m_m_y_e_e 在我的情况下,我正在获取 ADDED 案例中的所有文件。
  • 基本上,您的快照侦听器会为您提供您请求的数据,除此之外,它还会立即为您提供其中的任何更改。所以你添加的情况意味着一些数据已经添加到你正在接收的数据库中。

标签: python firebase firebase-realtime-database google-cloud-firestore


【解决方案1】:

在我看来唯一的解决方案是 - 您只需将 timestamp 附加到每个文档并根据时间查询文档。通过这种方式,您可以看到谁的时间戳(创建时间)是最新的,哪个是旧的。

希望一切顺利!

【讨论】:

  • 如果这不是我的答案,请发布该答案并将其标记为正确(供未来的读者使用),如果我的答案满足您的问题,请将其标记为正确。
【解决方案2】:

好吧,毕竟它在文档中!

for change in changes:
    if change.type.name == 'ADDED':
        print(f'New document: {change.document.id}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 2022-01-23
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    相关资源
    最近更新 更多