【问题标题】:Firestore collection group queries - how to order parent documents the subcollections are part of?Firestore 集合组查询 - 如何订购子集合所属的父文档?
【发布时间】:2021-01-27 08:17:39
【问题描述】:

我有以下数据结构:

collection/
    parent_document_1/
        timestamp: 0
        subcollection/
            document/
                belongsTo: parent_document_1
                data: data
    
    parent_document_2/
        timestamp: 0
        subcollection/
            document/
                belongsTo: parent_document_1
                data: data

    parent_document_3/
        timestamp: 0
        subcollection/
            document/
                belongsTo: parent_document_1
                data: data

我想通过timestamp 属性对parent_documents 进行排序,然后对subcollection 集合进行collectionGroup 查询。我不想向documents 添加其他属性,因为parent_documents 下的timestamp 值是我要排序的唯一值。原因是documents 可以相互重复,并且timestamp 需要为每个parent_document 单独更新。

更大的图景是我希望能够查询说,只有最近更新的 3-5 个subcollections。

这可能与 Firestore 相关吗?

【问题讨论】:

    标签: ios swift firebase google-cloud-firestore


    【解决方案1】:

    除了将时间戳复制到要查询的子集合中的文档中之外,您别无选择。

    使用 Firestore,当您查询集合或集合组时,您只能立即使用这些集合的文档中的字段。您可以不接触并使用其他集合中文档中的字段 - 这包括“父”文档和“子”文档。不同集合中的文档之间没有类似 SQL 的“连接”。

    【讨论】:

    • 感谢您的信息。猜猜我只需要将我的文档(应该只是简单的字符串)保存在文档节点下的数组中。
    • 请记住,由于 Firestore 文档具有最大大小,因此您将无法永远向该数组添加字符串。 Doug 的解决方案是首选/可扩展的,因为您可以在子集合中拥有的文档数量没有限制。
    • 感谢@Dan 我最终决定使用“存储桶”来存储最大数量的字符串,保证不会干扰最大文档大小。
    猜你喜欢
    • 2021-07-12
    • 2020-07-26
    • 2021-04-08
    • 1970-01-01
    • 2022-08-20
    • 2021-09-17
    • 2021-09-25
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多