我有一个数据库,其结构如下所示:
在这里,我需要有多个品牌,也有多个子品牌下的每个品牌。我以前试过使用“地图”,但它确实很混乱,而且我也了解到,最好有多个文档。我对此非常陌生,并且已经为之奋斗了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??
}
}
}
});
谢谢。
发布于 2018-03-18 17:24:32
这真的很简单,也很容易试一试。
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());
}
}
}
});
}
}
}});
备注:我更新了答案。很明显,不能使用单个请求在集合的文档中获取子集合。
发布于 2018-03-19 10:57:26
我知道这已经是一个公认的答案,但我想为您提供一种更优雅的方法来构造您的数据库。这意味着您还可以更容易地检索数据。在此之前,我从您的问题中了解到,您只想检索数据,但是为了实现这一点,只需使用get()
调用即可。addSnapshotListener()
用于检索real time
数据。
这是我建议您在应用程序中使用的数据库结构:
Firestore-root
|
--- coffee (collection)
|
--- coffeeId (document)
|
--- coffeeBrand: "Nescafe"
|
--- coffeeSubBrand: "Sunrise"
|
--- coffeeName: "CoffeeName"
|
--- quantity
|
--- 50g: true
|
--- 100g: true
|
--- 150g: true
尽管Nouman的代码可能适用于实际的数据库结构,但这种新结构将为您提供避免嵌套循环的能力。要获取所有咖啡,只需使用以下代码:
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");
如果您想要显示咖啡品牌和SubBrands,可以使用如下方式:
Query query = coffeeRef
.whereEqualTo("coffeeSubBrand", "Sunrise")
.whereEqualTo("coffeeBrand", "Nescafe");
如果您只想显示带有100g
数量的咖啡,那么只需使用以下查询:
Query query = coffeeRef.whereEqualTo("quantity.100g", true);
如果您想了解更多关于Cloud数据库结构的知识,可以查看我的教程之一。
https://stackoverflow.com/questions/49350776
复制相似问题