Firestore单查询:结合集合组与已知路径筛选特定城市及时间
Firestore 单查询实现特定城市下storetime时间戳过滤需求
问题背景
我的Firestore数据路径如下:country/wakanda/cityID/12345ABCDE/storetime/123456789
其中:
- 集合:country、cityID、storetime
- 文档:wakanda、12345ABCDE、123456789
查询前已知信息:country、wakanda、cityID、storetime
查询前未知信息:12345ABCDE、123456789
需求:编写查询语句,返回特定城市下,storetime文档时间戳大于X的所有数据。
当前实现与问题
我已实现仅筛选storetime大于X的查询:
val storesResponse = firestore.collectionGroup("storetime").whereGreaterThan(xField, xInSec).get().await()
但collectionGroup属于查询类型,无法关联到CollectionReference或DocumentReference,无法限定特定城市范围。
半解决方案及后续问题
感谢@Frank van Puffelen的回复,我按照其方案实现了特定城市的筛选,但无法同时实现storetime大于X的过滤。以下是供其他Android开发者参考的实现代码:
val countryRef = firestore.collection("country").document("wakanda") val storesAsync = firestore.collectionGroup("storetime") .orderBy(FieldPath.documentId()) .startAt(countryRef.path + "/") .endAt(countryRef.path + "\uf8ff").get() .addOnSuccessListener { Log.i(TAG, "Successfully read data, snapshot is: $it") }.addOnFailureListener { Log.i(TAG, "Failed to read data, exception is: $it") } val storesResponse = storesAsync.await()
现在尝试添加whereGreaterThan筛选条件时无法生效,想确认是否可以通过单查询实现需求,还是我的思路有误?
内容的提问来源于stack exchange,提问作者Red M
相关产品推荐
相关产品推荐

