【问题标题】:Limit results of GeoFirestore?限制 GeoFirestore 的结果?
【发布时间】:2019-01-11 16:05:01
【问题描述】:
我想限制 GeoFirestore 结果的结果,类似于 Firestore 中的功能。我尝试以各种方式在查询中使用limit(),但收到没有限制功能的错误。这不可能吗?
const geoFirestore = new GeoFirestore(firebase.firestore());
const geoCollectionRef = geoFirestore.collection('locations');
const query = geoCollectionRef.near({
center: new firebase.firestore.GeoPoint(39.76068, -104.98471),
radius: 10
});
query.get().then((value = GeoQuerySnapshot) => {
value.docs.forEach(doc => {
console.log(doc)
})
})
【问题讨论】:
标签:
firebase
google-cloud-firestore
geofirestore
【解决方案1】:
因此,这些附加功能(如限制)的问题在于,为了使查询正常工作,我们在执行 Geoquery 时会聚合多个查询。
我们使用您选择的区域周围的地理哈希创建一组查询。所以我们可以对每个查询应用限制,但是当聚合每个查询时都会应用限制,并且聚合会更大。因此,在客户端,我必须重新应用限制。这是非常可行的,但效率不是很高。
目前尚不支持它,但我希望在以后应用它。您可以做的一件事是以下...
import * as firebase from 'firebase';
import { GeoQuery, GeoQuerySnapshot } from 'geofirestore';
const collection = firebase.firestore().collection('somecollection');
// APPLY LIMIT HERE
const limitQuery = collection.limit(50);
const query = new GeoQuery(limitQuery).near({
center: new firebase.firestore.GeoPoint(39.76068, -104.98471),
radius: 10
});
query.get().then((value: GeoQuerySnapshot) => {
value.docs.forEach(doc => {
console.log(doc);
});
});
但是在这种情况下,地理查询的每个查询都会应用限制,而不是聚合。您可以修改您想要的限制数量,以便在客户端获得接近的结果。 (您会在 2 月的某个时候看到此功能正确集成)