0

The more I read on this topic the more I am confused.
I need small clarification.
If I convert my coordinates to geohash like: c2b2aaa using some library that can do that.
And in firestore collection documents I have field
.

doc: {... geohash: 'c2b2qdd'}
doc: {... geohash: 'c2b2abd'}
doc: {... geohash: 'c2b2ahd'}
doc: {... geohash: 'c2b2jjd'}

firestore().collection('myCollection').orderBy('geohash', '>=', 'c2b2aaa').limit(100);

Will this query return me up to 100 documents sorted by distance from my location 'c2b2aaa'?
Or I am too naive and think this would work.

1110
  • 7,829
  • 55
  • 176
  • 334
  • Second one naive. – Mises Jul 25 '22 at 20:33
  • You can use geocaches to check if someone is in a certain sector of a map. – Mises Jul 25 '22 at 20:36
  • I need to get data from firebase from collection but closer to my coordinates. If in first page I get data that are 50km faraway I can sort in app but again the closest will ve 50km away. Is there possibility to do this somehow? – 1110 Jul 25 '22 at 20:40
  • Yes, but you need to make a complicated algorithm. – Mises Jul 25 '22 at 20:43
  • Well if you want to be precise, if not you can take first 4 chars and search according to just those. The problem is that person who with those 4 first chars will be in the corner of that sector. You will get for example just places according to north-west where he is. – Mises Jul 25 '22 at 20:54
  • Here you have library and everything you need to perform on client side to get correct docs according to location using geohashes. https://firebase.google.com/docs/firestore/solutions/geoqueries Or you can convert it to use it in Firebase functions for example. – Mises Jul 26 '22 at 04:33
  • @Mises thanks for pointing me I am step further in understanding this. I see the problem about N & NW. I don't need to be precice but just not to get locations from germany if I am in london. I think 3chars are fine for me (156km). I am thinking to find all neighbors for my location geohash which means 8 more queries and now I am thinking how to avoid this and pack it in single query... any idea? I am avoiding geofire as I need compound queries `all locations around me where status == 8` for example/ – 1110 Jul 26 '22 at 11:37

1 Answers1

0

For Firestore Data model perspective, what you'll be doing with your sample document structure is possible but your query does have some issue with it. You need to use the where() method when comparing values and use the orderBy() method on the field you want to sort (you can also sort it ascending or descending). See sample code below:

firestore()
.collection('myCollection')
.where('geohash', '>=', 'c2b2aaa')
.orderBy('geohash', '<asc> or <desc>')
.limit(100)
.get()
.then((querySnapshot) => {
    querySnapshot.forEach((doc) => {
        // doc.data() is never undefined for query doc snapshots
        console.log(doc.id, " => ", doc.data());
    });
})
.catch((error) => {
    console.log("Error getting documents: ", error);
});

For more information on how to sort data from Firestore, you check out Order and limit data with Cloud Firestore.


Regarding the Geohash, there are related threads that you might want to check to further help you:

I would also recommend you to watch this video regarding Querying Firebase and Firestore based on geographic location or distance

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
  • You may also want to check this [thread](https://stackoverflow.com/questions/56213015/ordering-firestore-geohash-query-from-closest-to-furthest). – Marc Anthony B Jul 26 '22 at 02:38
  • Nope, it's not possible. He needs to first get to know what queries he needs to perform, then query them all, so he gets documents in range/area. If he wants to sort the result according to distance, he needs to count the distance to every document according to his position. – Mises Jul 26 '22 at 03:30
  • @Mises , I just based on the sample document structure provided which contains strings that can be compared and ordered by its field but as I mentioned, there are threads regarding how to properly query and order data by distance with Geohash. – Marc Anthony B Jul 26 '22 at 03:57
  • Your answer looks confuse. Someone may not understand that you only shows/explain he wrote bad request. Someone may understand that this is an example of requests he needs to perform to get documents in a range. – Mises Jul 26 '22 at 04:14
  • Thanks for the heads up. I'll just clarify it on my answer. – Marc Anthony B Jul 26 '22 at 05:16