[英]How to retrieve documents of a sub collection in Firestore?
在這里,我需要擁有多個品牌,每個品牌下還有多個子品牌。 我之前嘗試使用'地圖',但它確實非常混亂,並且還了解到擁有多個文檔會更好。 我對此非常陌生,現在已經掙扎了3-4天。 任何幫助將非常感激。
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??
}
}
}
});
謝謝。
這非常簡單易用。
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());
}
}
}
});
}
}
}});
注意 :我已經更新了答案。 很明顯,您無法使用單個請求獲取集合文檔中的子集合。
我看到這已經是一個公認的答案,但我想為您提供一種更優雅的數據庫結構化方法。 這意味着您還可以更輕松地檢索數據。 在此之前,正如我從您的問題中理解的那樣,您只想檢索數據,但為了實現這一點,只需使用get()
調用即可。 addSnapshotListener()
用於檢索real time
數據。
這是我建議您用於應用程序的數據庫結構:
Firestore-root
|
--- coffee (collection)
|
--- coffeeId (document)
|
--- coffeeBrand: "Nescafe"
|
--- coffeeSubBrand: "Sunrise"
|
--- coffeeName: "CoffeeName"
|
--- quantity
|
--- 50g: true
|
--- 100g: true
|
--- 150g: true
雖然Nouman Ch的代碼可能適用於您的實際數據庫結構,但這個新結構將為您提供避免嵌套循環的能力。 要獲得所有的咖啡,只需使用以下代碼:
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
:
Query query = coffeeRef.whereEqualTo("coffeeBrand", "Nescafe");
如果您只想顯示咖啡SubBrands,請使用以下查詢:
Query query = coffeeRef.whereEqualTo("coffeeSubBrand", "Sunrise");
如果你想顯示咖啡品牌和子品牌,你可以鏈接這樣的方法:
Query query = coffeeRef
.whereEqualTo("coffeeSubBrand", "Sunrise")
.whereEqualTo("coffeeBrand", "Nescafe");
如果您只想顯示數量為100g
的咖啡,請使用以下查詢:
Query query = coffeeRef.whereEqualTo("quantity.100g", true);
如果您想了解有關Cloud Firestore數據庫結構的更多信息,可以查看我的一個教程 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.