gpt4 book ai didi

java - 如何使用where子句从firestore中的集合中获取数据?

转载 作者:行者123 更新时间:2023-12-02 03:46:14 25 4
gpt4 key购买 nike

我想使用 where 子句从 firestore 中的集合中获取数据(经度字段),但它不起作用。

我的代码:

db = FirebaseFirestore.getInstance();
Query query = db.collection(collection).whereGreaterThan("longtitude", String.valueOf(bounds.northeast.latitude));
query.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}

if (queryDocumentSnapshots != null) {
Log.d(TAG, queryDocumentSnapshots.getMetadata().toString());
} else {
Log.d(TAG, "Current data: null");
}
}
});

我只得到

SnapshotMetadata{hasPendingWrites=false, isFromCache=false}

但我想要集合中的字段。怎么做?

我尝试了不同的方法,但没有一个返回集合中的字段。

最佳答案

当您对集合运行查询时,您会得到一个QuerySnapshot,其中可以包含零个或多个文档。要获取实际文档,您需要循环遍历文档,如下所示 here :

public void onEvent(@javax.annotation.Nullable QuerySnapshot queryDocumentSnapshots, @javax.annotation.Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}

for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
if (doc.get("longtitude") != null) {
Log.d(TAG, doc.getString("longtitude"));
}
}
}

关于java - 如何使用where子句从firestore中的集合中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56805177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com