Java Firebase Cloud Firestore-从偶数个片段中获取集合
我有一个这样的数据库Java Firebase Cloud Firestore-从偶数个片段中获取集合,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我有一个这样的数据库Shop>warehouse>categories>cat1、cat2,…,每个类别都有一个名称及其文档。我只想要类别,我尝试使用以下代码: firestore.collection("shop/warehouse").addSnapshotListener(new EventListener<QuerySnapshot>() { @Override public void onEvent(@javax.annotation.Nul
Shop>warehouse>categories>cat1、cat2,…
,每个类别都有一个名称及其文档。我只想要类别,我尝试使用以下代码:
firestore.collection("shop/warehouse").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if(queryDocumentSnapshots != null) {
for (DocumentSnapshot doc : queryDocumentSnapshots) {
String item = doc.getId();
Log.d("TEST ITEM", item);
}
} else Log.d(TAG, "queryDocumentSnapshots is null.");
}
});
所以我在谷歌上搜索并找到了我写的:
firestore.collection("shop/").document("warehouse").addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable DocumentSnapshot documentSnapshot, @javax.annotation.Nullable FirebaseFirestoreException e) {
Map<String, Object> map = documentSnapshot.getData();
Log.d("size", map.keySet().size()+"");
}
firestore.collection(“shop/”).document(“warehouse”).addSnapshotListener(新的EventListener(){
@凌驾
public void onEvent(@javax.annotation.Nullable DocumentSnapshot DocumentSnapshot,@javax.annotation.Nullable FirebaseFirestoreException e){
Map Map=documentSnapshot.getData();
Log.d(“size”,map.keySet().size()+”);
}
但是这个密钥集(我假设它们是类别名称)是空的。我该怎么做?饶了我吧,这是我第一次使用Firebase!要解决这个问题,请更改以下代码行:
firestore.collection("shop/").document("warehouse").addSnapshotListener(/* ... */);
到
不需要这样的
/
。您可以使用这样的参考资料,“商店/仓库”
仅适用于Firebase实时数据库,其中所有对象都称为Child,您可以在单个路径中链接所有Child。这是一种更优雅的方式,但不幸的是,在Cloud Firestore中不允许使用。没有API仅列出集合中存在的文档ID。您必须查询并读取c中的所有文档收集只是为了获取ID。可能重复的@Gastónsaillen不是重复的,我尝试了下面这个问题的解决方案,但它们不起作用。您有此错误无效的收集引用。收集引用的段数必须是奇数,但使用firestore.collection时,商店/仓库有2个(“车间”)文件(“仓库”)
?一切正常吗,问题解决了吗?不,我还是很遗憾地发现这个错误,删除“\”什么都没有改变。请添加您的数据库结构。看到您的整个数据库结构,我可以说,在不查询数据库的情况下,您无法仅获取这些ID。正如Doug所说,您需要查询整个数据库来读取所有这些ID我不知道你所说的“暴力”是什么意思,但正如我在回答中所说的,你必须查询整个集合,读取所有文档,并获取它们的所有ID。对不起,我的意思是硬编码,因为当我查询文档时,我得不到任何数据,这里是一个例子(很抱歉质量很低,我不知道为什么这么差)不管你在那里做什么,看起来都与原来的问题无关,
firestore.collection("shop/").document("warehouse").addSnapshotListener(/* ... */);
firestore.collection("shop").document("warehouse").addSnapshotListener(/* ... */);