title: How to filter Firestore collection data using function (for location distance filtering)
body: <p>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.</p>
  
  <p>Suppose had some Firestore collection of documents of the form</p>
  
  <pre><code>{
    pointOfInterest: "Some string label"
    longitude: -100.123
    latitude: 50.456
  }
  </code></pre>
  
  <p>Also have some code that retrieves a user's geocoordinates (in my case, via react-native), so something like</p>
  
  <pre class="lang-js prettyprint-override"><code>const getCurrentLatLong = () =&gt; {
      // do some stuff, then...
      return { latitude: someNumber, longitude: someOtherNumber }
  }
  </code></pre>
  
  <p>What would ultimately like to be able to do is run something like</p>
  
  <pre class="lang-js prettyprint-override"><code>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) 
               &lt; SOME_CONST_DISTANCE
       })
  </code></pre>
  
  <p>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.</p>
  
  <p>Some googling led to some potential starting points (<a href="https://stackoverflow.com/a/45359952/8236733">https://stackoverflow.com/a/45359952/8236733</a> and <a href="https://firebase.google.com/docs/reference/js/firebase.database.Query#on" rel="nofollow noreferrer">https://firebase.google.com/docs/reference/js/firebase.database.Query#on</a>), 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. </p>