简体   繁体   English

在 QueryDocumentSnapshot 中获取集合的所有文档

[英]get all the documents of a collection inside a QueryDocumentSnapshot

enter image description here在此处输入图片说明

/Info/Semestre1/Courses/course1 Is my full data base reference. /Info/Semestre1/Courses/course1 是我的完整数据库参考。 I'm trying to access all the courses name of all the semesters at ounce.我正在尝试以 ounce 访问所有学期的所有课程名称。

 db.collection("Courses").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {

                        for (QueryDocumentSnapshot document : task.getResult()) {





                            }

                          // Log.d(TAG, document.getId() + " => " + document.getData());



                        }



            }
        });


    }


I

I got all the semesters of Info.我得到了所有学期的信息。 Now how can I access all the courses names?现在如何访问所有课程名称?

You are doing the same mistake which i done when i was learning more about firestore.当我更多地了解 firestore 时,你犯了同样的错误。

So the first mistake you are doing is directly accessing the "Courses" collection which所以你做的第一个错误是直接访问“课程”集合

is not possible if you want to access the "Courses" collection then you must need to traverse through all the collection and document before it.如果您想访问“课程”集合是不可能的,那么您必须遍历它之前的所有集合和文档。

Ex :collection1/doc1/collection2/doc2/collection3例如:collection1/doc1/collection2/doc2/collection3

Now if you want to access the data of the collection3 then query for that is going to be like现在,如果您想访问 collection3 的数据,那么查询将类似于

collection1.doc().collection2.doc2().collection3.get() and then your listner thats just basic understanding before we move for you answer. collection1.doc().collection2.doc2().collection3.get() 然后你的听众在我们为你回答之前只是基本的了解。

Now ans of you question现在回答你的问题

 db.collection("info").document("sem-id").collection(Courses).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {

                    for (QueryDocumentSnapshot document : task.getResult()) {





                        }

                      // Log.d(TAG, document.getId() + " => " + document.getData());



                   }

        }
    });


}

Let me know if still any doubt.Happy coding如果还有任何疑问,请告诉我。快乐编码

I think your query in the question is actually incorrect.我认为您在问题中的查询实际上是不正确的。 Based on what I see in your screenshot, to get all the semesters, you would do this:根据我在您的屏幕截图中看到的内容,要获得所有学期,您可以这样做:

db.collection("Info").get()

Then, to get all the courses for a specific semester in the subcollection nested under the semester, you would need a whole new query like this:然后,要在学期下嵌套的子集合中获取特定学期的所有课程,您需要一个全新的查询,如下所示:

String courseId = ...  // changes as you iterate the courses in Info
db.collection("Info").document(courseId).collection("Courses").get()

You can't get all the semesters and courses in one query with the database structure you have now.您无法使用您现在拥有的数据库结构在一次查询中获得所有学期和课程。 Firestore queries are shallow, meaning they don't consider subcollections at all. Firestore 查询很浅,这意味着它们根本不考虑子集合。 Each subcollection requires its own separate query.每个子集合都需要自己单独的查询。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM