0

I would like to limit the results of GeoFirestore results similar to the capability within Firestore. I have tried to use limit() on the query in various fashions but receive an error of no limit function. Is this not possible?

  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)
    })
  })
4

1 回答 1

0

So the problem with these additional sorts of function, like limit, is that for the queries to work we aggregate multiple queries when you do a Geoquery.

We create an array of queries with geohashes that are around the area that you selected. So we could apply the limit to each of those queries, but when aggregated each query would have the limit applied to it, and the aggregate would be larger. So then on the client side I have to reapply the limit. It's very much doable, it is not very much of efficient.

As of right now it isn't supported, but I am looking to apply it down the road. One thing you could do is the following...

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);
  });
});

However in this case each query of the geoquery would have the limit applied, not the aggregate. You can tinker with the limit number you want in order to get something close on the client side. (You'll see this feature properly integrated sometime in February)

于 2019-01-11T23:14:57.367 回答