简体   繁体   English

Android Studio-如何从Firestore的子集合中获取文档?

[英]Android Studio - How do I get documents from a sub-collection in Firestore?

I would like to get documents in a sub-collection where the date=DATE and title=TITLE in a document. 我想在文档中date = DATE和title = TITLE的子集合中获取文档。 I'm using date and title as primary keys so that I can display information from the documents from sub-collection "bulletin". 我将日期和标题用作主键,以便可以显示来自子集合“公告”的文档中的信息。 How would I do that in Android Studio? 我将如何在Android Studio中做到这一点?

Or would it be better to use a different structure? 还是使用其他结构会更好?

Thanks! 谢谢!

Image - Firestore Structure 图像-消防站结构

To solve this, please use the following lines of code: 要解决此问题,请使用以下代码行:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference bulletinRef = rootRef.collection("bulletin");
bulletinRef.whereEqualTo("date", date).whereEqualTo("title", title).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.getString("title"));
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});

The result in your logcat will be, all titles from all documents within your bulletin collection. 日志记录中的结果将是bulletin集中所有文档的所有标题。

暂无
暂无

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

相关问题 如何从Android上的Firestore中的子集合中获取数据? - How to get data from a sub-collection in Firestore on Android? Android:从子集合中获取字段值 - Android: Get field values from sub-collection 如何读取 firestore 子集合并将其传递给 FirestoreRecyclerOptions - how to read firestore sub-collection and pass it to FirestoreRecyclerOptions 如何从 Firestore Android 中的集合中获取文档列表 - How to get list of documents from a collection in Firestore Android 如果我使用Query在“Android”中拥有用户ID arraylist,如何从“Firestore”集合中获取特定的文档列表 - How to get specific list of documents from “Firestore” collection if i have users ID arraylist in “Android” using Query 在 Users 集合中创建一个朋友子集合,Cloud Firestore - Create a friend sub-collection in the Users collection, Cloud Firestore 如何在 Firestore 的不同子集合中简化我的代码添加/更新文档? - How to simplify my code add/update Document in different sub-Collection in Firestore? 如何获取已传递集合的子集合 <generic type> 成员在哪里扩展或实施另一个? - How to get a sub-collection of a passed collection<generic type> where members extend or implement another? 如何将两个集合添加到 Firestore 的集合 android 工作室中 - How can I add two collection into collection android studio in firestore 如何从Firestore中的集合中检索所有文档? - How to retrieve all documents from a collection in Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM