Firestore集合组查询-如何订购子集合所属的父文档?

时间:2020-10-13 02:33:10

标签: ios swift firebase google-cloud-firestore

我具有以下数据结构:

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

我想按parent_documents的{​​{1}}属性对其进行排序,然后在timestamp集合上进行collectionGroup查询。我不想向subcollection添加其他属性,因为documents下的timestamp值是我要排序的唯一值。这样做的原因是parent_documents可以彼此重复,并且documents需要分别为每个timestamp更新。

更大的情况是,我希望能够仅查询最近更新的3-5个parent_document

这可能与Firestore有关吗?

1 个答案:

答案 0 :(得分:1)

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

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

相关问题