Firebase-Firestore按时间顺序获取带有地理点和时间戳的数据

Firebase-Firestore按时间顺序获取带有地理点和时间戳的数据,firebase,firebase-realtime-database,google-cloud-firestore,google-cloud-functions,Firebase,Firebase Realtime Database,Google Cloud Firestore,Google Cloud Functions,我目前正在从事firebase项目。在这种情况下,我们决定使用Firestore。 为了简单起见,假设我们有多个应用程序用户在给定的时间将他们的位置发布到服务器,因此我们的文档看起来像这样 Footprint -> username -> createdAt (timestamp in milliseconds) -> location (Geopoint) 现在,用户应该能够搜索他周围的所有用户,通过使用如下查询,这非常容易做到: firestore.collection

我目前正在从事firebase项目。在这种情况下,我们决定使用Firestore。 为了简单起见,假设我们有多个应用程序用户在给定的时间将他们的位置发布到服务器,因此我们的文档看起来像这样

Footprint
-> username 
-> createdAt (timestamp in milliseconds)
-> location (Geopoint)
现在,用户应该能够搜索他周围的所有用户,通过使用如下查询,这非常容易做到:

firestore.collection('footprint')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
边界['lower']和边界['upper']包含2个地质点

现在,这将是可行的,但它提供了一个地质点附近的所有数据。但用户只关心它的时间顺序。更像是这个位置附近的最后100个人。因此,仅仅为了在客户端订购而向用户发送10000个数据集似乎很糟糕。通常没有可用的Wi-Fi

因此,我想对结果进行分页,并尝试了以下方法:

firestore.collection('footprint')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
.orderBy('createdAt')
.limit(25)
不起作用,在这种情况下,Firebase首先强制执行orderBy“location”。但结果是

firestore.collection('footprint')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
.orderBy('location')
.orderBy('createdAt')
.limit(25)
这对我没有帮助。如果数据集就在我当前位置的旁边,它会首先显示2017年的数据集

因此,我考虑了替代方案,发现如下:

firestore.collection('footprint')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
.orderBy('createdAt')
.limit(25)
对地点进行聚类 我想我可以对位置进行聚类,比如将17.13-17.17到17.15之间的所有位置进行映射,然后我可以使用equals查询来获得我想要的数据

firestore.collection('footprint')
.where('location', isEqual: bounds['closest'])
.orderBy('createdAt')
.limit(25)
但是现在我遇到了一个问题,如果用户想要方圆100公里内的所有东西,我将不得不进行大量的查询。考虑使用5公里的集群,对于半径为100公里的所有集群来说,这将是40*40的查询。即使这样,我也无法限制这些数据,因为我关心时间顺序,我仍然需要向用户发送大约10000个数据集

使用附加日期或类似字段 我觉得这是我最近一次为我的问题找到真正的解决办法

Footprint
-> username 
-> createdAt (timestamp in milliseconds)
-> createdSearch (String looks like 2018-06-14)
-> location (Geopoint)
我现在可以查询给定日期的数据,如

firestore.collection('footprint')
.where('createdSearch',isEqual:'2018-06-14')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
.orderBy('location')
这是可行的,但我每天都要发送额外的查询,而且我必须在客户端订购数据,所以没有限制。如果我有大量数据,我还需要使我的搜索字段更明确,如“2018-06-14 12:00”。但是我必须发送更多的查询

3尝试将地质点和时间戳合并到一个字段中

这在理论上应该是可行的,如果我找到一种方法将两个值合并到一个字段中,允许查询它们并将它们与同一个字段排序,这将是可行的。但我不知道怎么做

我觉得这应该是一个常见的问题,我发现了一些关于范围查询和按其他字段排序的其他问题,但与地理位置无关

那么,在给定位置附近查询数据集并使用Firestore按时间顺序取回数据集的generell方法是什么呢


我还应该提到,我不希望用户等待30秒,所以如果可能的话,这应该是一个快速的解决方案。如果需要其他技术firebase或数据结构,也可以。

如果找到解决问题的唯一好方法,就是使用firebase函数的最后一个变体

firestore.collection('footprint')
.where('createdSearch',isEqual:'2018-06-14')
.where('location', isGreaterThan: bounds['lower'])
.where('location', isLessThan: bounds['upper'])
.orderBy('location')

因此,应用程序向firebase函数发出HTTPS请求,该函数加载所有内容,但执行一些服务器端过滤以减少返回值的数量。由于这不是我特定问题的真正解决方案,我不会将其标记为回答。

使用firebase函数的处理是否算作对数据库的额外读/写?如何向firebase函数发出HTTPS请求?我以为它们只是在数据库积垢上触发的。