首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Firestore中检索子集合的文档?

如何在Firestore中检索子集合的文档?
EN

Stack Overflow用户
提问于 2018-03-18 17:20:46
回答 2查看 5.9K关注 0票数 6

我有一个数据库,其结构如下所示:

在这里,我需要有多个品牌,也有多个子品牌下的每个品牌。我以前试过使用“地图”,但它确实很混乱,而且我也了解到,最好有多个文档。我对此非常陌生,并且已经为之奋斗了3-4天了。任何帮助都将不胜感激。

代码语言:javascript
运行
复制
  firebaseFirestore.collection("Coffee").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null) {
                Log.d(TAB, "Error : " + e.getMessage());
            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED) {
                    Log.d("Brand Name: ",doc.getDocument().getId());

                  //  how to retrieve documents of subCollection here? something like doc.getDocument().collection??

                }

            }

        }
    });

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-18 17:24:32

这真的很简单,也很容易试一试。

代码语言:javascript
运行
复制
 firebaseFirestore.collection("Coffee").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (e != null) {
                Log.d("", "Error : " + e.getMessage());
            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                if (doc.getType() == DocumentChange.Type.ADDED) {
                    Log.d("Brand Name: ", doc.getDocument().getId());
                    doc.getDocument().getReference().collection("SubBrands").addSnapshotListener(new EventListener<QuerySnapshot>() {
                        @Override
                        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
                            if (e != null) {
                                Log.d("", "Error : " + e.getMessage());
                            }

                            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                                if (doc.getType() == DocumentChange.Type.ADDED) {
                                    Log.d("SubBrands Name: ", doc.getDocument().getId());
                                }
                            }

                        }
                    });
                }

            }
        }});

备注:我更新了答案。很明显,不能使用单个请求在集合的文档中获取子集合。

票数 3
EN

Stack Overflow用户

发布于 2018-03-19 10:57:26

我知道这已经是一个公认的答案,但我想为您提供一种更优雅的方法来构造您的数据库。这意味着您还可以更容易地检索数据。在此之前,我从您的问题中了解到,您只想检索数据,但是为了实现这一点,只需使用get()调用即可。addSnapshotListener()用于检索real time数据。

这是我建议您在应用程序中使用的数据库结构:

代码语言:javascript
运行
复制
Firestore-root
   |
   --- coffee (collection)
         |
         --- coffeeId (document)
                |
                --- coffeeBrand: "Nescafe"
                |
                --- coffeeSubBrand: "Sunrise"
                |
                --- coffeeName: "CoffeeName"
                |
                --- quantity
                       |
                       --- 50g: true
                       |
                       --- 100g: true
                       |
                       --- 150g: true

尽管Nouman的代码可能适用于实际的数据库结构,但这种新结构将为您提供避免嵌套循环的能力。要获取所有咖啡,只需使用以下代码:

代码语言:javascript
运行
复制
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference coffeeRef = rootRef.collection("coffee");
coffeeRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot document : task.getResult()) {
                String coffeeName = document.getString("coffeeName");
                Log.d("TAG", coffeeName);
            }
        }
    }
});

如果只想显示Nescafe coffee,而不是使用CollectionReference,则需要使用Query

代码语言:javascript
运行
复制
Query query = coffeeRef.whereEqualTo("coffeeBrand", "Nescafe");

如果只想显示咖啡SubBrands,请使用以下查询:

代码语言:javascript
运行
复制
Query query = coffeeRef.whereEqualTo("coffeeSubBrand", "Sunrise");

如果您想要显示咖啡品牌和SubBrands,可以使用如下方式:

代码语言:javascript
运行
复制
Query query = coffeeRef
    .whereEqualTo("coffeeSubBrand", "Sunrise")
    .whereEqualTo("coffeeBrand", "Nescafe");

如果您只想显示带有100g数量的咖啡,那么只需使用以下查询:

代码语言:javascript
运行
复制
Query query = coffeeRef.whereEqualTo("quantity.100g", true);

如果您想了解更多关于Cloud数据库结构的知识,可以查看我的教程之一。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49350776

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档