简体   繁体   English

我可以在 Cloud Firestore 的子集合中查询和检索文档的字段值吗?

[英]Can I query and retrieve Documents' field values in Subcollection in Cloud Firestore?

Can I query and retrieve the Documents' field values in a subcollection?我可以在子集合中查询和检索文档的字段值吗?

Take the following for example:以以下为例:

States [Collection]
    State 1 [Document]
        Counties [Collection]
           County 1 [Document]
               British Population = 100 [Field]

           County 2 [Document]
               British Population = 200 [Field]

    State 2 [Document]
        Counties [Collection]

           County 1 [Document]
               British Population = 150 [Field]

How do I query the Documents with British Populations and retrieve the number of the population ?如何查询具有英国人口的文档并检索population

Since the name of the Counties subcollection is the same within all State documents, to get the value of the "British Population" property, a Firestore collection group query is required.由于Counties子集合的名称在所有State文档中都相同,因此要获取“British Population”属性的值,需要Firestore 集合组查询 However, this will not work unless you have another property under your Country document that can help you create that query.但是,除非您的Country文档下有另一个属性可以帮助您创建该查询,否则这将不起作用。 In code, should look like this:在代码中,应该如下所示:

db.collectionGroup("Counties").whereEqualTo("nationality", "British").get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                // ...
            }
        });

This is translated like this.这是这样翻译的。 Hey Firestore, give me all Country documents that exist in all Countries collections or subcollections where "nationality" is "British".嘿 Firestore,给我所有Countries collections 或“国籍”为“英国”的子集合中存在的所有Country文件。 So as you can see, I have added a new property named nationality and added the corresponding value as "British".如您所见,我添加了一个名为nationality的新属性,并将相应的值添加为“British”。 Since all documents do contains such property, all documents will be returned.由于所有文档都包含此类属性,因此将返回所有文档。

Besides that, the "British Population" property can also be changed to "population".除此之外,“英国人口”属性也可以更改为“人口”。 Now, inside the onSuccess() , you can query the QuerySnapshot object and get the value for the population property.现在,在onSuccess()中,您可以查询QuerySnapshot object 并获取population属性的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以使用 Firestore 检索具有相同名称的不同集合中相同子集合的文档吗? - Can I retrieve documents of same subcollection in different collections with the same name, using Firestore? 如何使用 spring 框架检索 Firestore 中的子集合? - How can I retrieve a subcollection in firestore with spring framework? 如何使用 Android 从 Cloud Firestore 中的地图中检索特定字段? - How can I retrieve a specific field from a map in Cloud Firestore using Android? 我想将用户数据存储为 Cloud Firestore 中管理文档的子集合 - I want to store user data as a subcollection of the admin document in the Cloud Firestore 从 Android 中的 Firestore 检索子集合数据 - Retrieve subcollection data from Firestore in Android 如何将子集合添加到 Firebase Cloud Firestore 中的文档 - How to add subcollection to a document in Firebase Cloud Firestore 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? 如何访问 Firestore 子集合中的字段值 - How to access the values of the fields in a Firestore Subcollection Firestore:如何在Firestore中创建子集合,而不必添加文档 - Firestore: How can I create a subcollection in Firestore without having to add a document already 如何计算 Cloud Firestore 中查询过滤的文档数 - How to count the number of documents filtered by a query in Cloud Firestore
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM