How to filter Firestore collection data using function (for location distance filtering)

强颜欢笑 提交于 2020-04-18 12:20:53

问题


Would like to know how to filter a Firestore collection of docs using a function where some of the function args are the values of the collection documents.

Suppose had some Firestore collection of documents of the form

{
  pointOfInterest: "Some string label"
  longitude: -100.123
  latitude: 50.456
}

Also have some code that retrieves a user's geocoordinates (in my case, via react-native), so something like

const getCurrentLatLong = () => {
    // do some stuff, then...
    return { latitude: someNumber, longitude: someOtherNumber }
}

What would ultimately like to be able to do is run something like

let currentLocation = getCurrentLatLong()
let filteredSet = firebase
    .firestore()
    .collection('MyCollection')
    // filtering each individual document 
    .filter(function (document, currentLocation) {
         let docLat = document.latitude
         let docLong = document.longitude
         return distance(
             {latitude: docLat, longitude: docLong},
             currentLocation) 
             < SOME_CONST_DISTANCE
     })

So that end up with some filteredSet of all the documents in the collection that have some distance from the currentLocation less than SOME_CONST_DISTANCE.

Some googling led to some potential starting points (https://stackoverflow.com/a/45359952/8236733 and https://firebase.google.com/docs/reference/js/firebase.database.Query#on), but not quite sure how to use what's being shown in the links. Any advice or docs on how to achieve this would be appreciated.


回答1:


There is no way to pass a function into Cloud Firestore that it then uses to filter the documents that it returns. You either need to pass in static values (e.g. the latitude and longitude you want back), or you will need to read all documents and filter with the function client-side.

What you're trying to do is known as a geoquery: returning documents based on their distance to a known point. Unfortunately that functionality is not built into Cloud Firestore yet, even though it has support for a geographical point data type. It is possible to build it yourself, though you should realize such queries are quite inefficient at the moment.

To learn more about this:

  • Some developers have ported Firebase's GeoFire library to Firestore. See How to run a geo "nearby" query with firestore?,
  • I gave a talk about this a while ago, which is available on youtube: https://www.youtube.com/watch?v=mx1mMdHBi5Q
  • AngularFirebase.com also has a page on Realtime GeoQueries With Firestore



回答2:


So as answered previously, Firestore doesn't support native geo queries. This is a bit of a shameful plug for some open source stuff I've been working on...

https://github.com/mbramwell1/GeoFire-Android

That lets you do standard Firestore queries, but also adds in the ability to search by location aswell. Give it a look over it may do what you want.



来源:https://stackoverflow.com/questions/53607847/how-to-filter-firestore-collection-data-using-function-for-location-distance-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!