通过自定义对象字段查询Google Firestore数据库 [英] Query Google Firestore database by custom object fields
问题描述
我具有以下Google Firestore数据库结构:
I've got the following Google Firestore database structure:
是否有一种方法可以按一定范围(纬度和经度)进行查询?例如,我要获取所有 location.lat
和 location.lng
在以下范围内的用户: 51.400
到 52.000
和 -0.100
到 -0.300
.谁能帮我找出执行此操作所需的查询吗?
Is there a way to query by a range of each - latitude and longitude? For example I want to get all users which have location.lat
and location.lng
in the range of: 51.400
to 52.000
and -0.100
to -0.300
. Can anyone please help me figure out the query needed to do that?
我希望这个问题有意义.任何帮助表示赞赏.
I hope this question makes any sense. Any help appreciated.
我没有使用地理位置"数据类型.我正在使用具有2个浮点数的自定义对象,并且想要进行简单的数字比较.问题是我不知道(也找不到)如何构建查询的方法.
I am not using a 'Geographical point' data type. I am using a custom object with 2 float numbers and want to do a simple number compare. The thing is that I do not know (and cannot find) a way how to build my query.
推荐答案
这应该是可能的
userRef.where('location.lat', '>=', 51.400).where('location.lat', '<=', 52.000);
尽管我认为如果不在同一字段上,您可以在其上链接更多的 .where
.
Though I don't think you can chain more .where
on it if it's not on the same field.
您可以尝试对其下方的 location.lng
进行类似的查询,然后比较和匹配这两个查询中的用户.或检查 location.lng
是否在代码中所需的范围内,并过滤掉这些用户.无论如何,您最终会从数据库中获得比期望更多的结果,然后丢弃一些结果.
You could try to do a similar query for location.lng
beneath it and then compare and match the users from both queries. Or check if the location.lng
is in the desired range in your code and filter out those users. In any case you'll end up getting more results from the database than preferred and then discarding some.
这篇关于通过自定义对象字段查询Google Firestore数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!