[英]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! 谢谢!
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.