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