Querying Firebase and Firestore

SFHTML5 · 24K views · 2018 · 1:01:42

Open on YouTube

Databases & storageWeb developmenttalk

Why Stack Overflow recommends it

Stack Overflow users link it when asked how to run nearby or radius queries in Firestore, since it explains why native queries fall short and how geohashes (as used by GeoFire) solve it.

Recommended by 13 people across 37 different months, 2017 to 2024.

What it is

A one-hour SFHTML5 meetup talk by Frank van Puffelen of Google's Firebase team on running location-based queries in the Firebase Realtime Database and Cloud Firestore. He explains geolocation, geohashes and geoqueries, with live JavaScript examples in JSBin.

What people said

Strong rec Frank van Puffelen · 2022-06 · Stack Overflow · on “Is it possible to query Firebase Firestore by both a location and age range?”

Firestore (and many other NoSQL databases) can only perform relational conditions, such as isGreaterThanOrEqualTo / isLessThanOrEqualTo / startAt / endAt , on a single field, due to how their indexes work behind the scenes. To allow you to perform geoqueries on such a database, libraries such as GeoFire use so-called GeoHash values, which magically merge the two latitude and longitude values into a single value (the g in your data structure) that you can perform a range condition on. It's quite magical really. I did a talk on the topic a few years ago, which I highly recommend checking out: Geo-querying Firebase and Firestore [this video] . Now if you'd also like to filter on another property suc

Strong rec Jul Pod · 2023-09 · Stack Overflow · on “firebase generic filtering needs indexes for each combination”

i will answer the question by my own and maybe for somebody who is also stepping across this situation: in general its not possible to filter a collection as i did as in a in a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field - in my case i needed it for four range filter fields. after checking the docs and some other repos i saw that in my situation it is the best to use only one range filter for the geoposition, to make this happen i use GeoFire which uses geohashes so only one field is required for the range filter. Please check out this video [this video] it is explained brilliant how to query for geopoints in firebase let query: a

Recommends Ryan Lee · 2017-10 · Stack Overflow · on “How to run a geo "nearby" query with firestore?”

UPDATE: Firestore does not support actual GeoPoint queries at present so while the below query executes successfully, it only filters by latitude, not by longitude and thus will return many results that are not nearby. The best solution would be to use geohashes [LINK] . To learn how to do something similar yourself, have a look at this video [this video] . This can be done by creating a bounding box less than greater than query. As for the efficiency, I can't speak to it. Note, the accuracy of the lat/long offset for ~1 mile should be reviewed, but here is a quick way to do this: SWIFT 3.0 Version func getDocumentNearBy(latitude: Double, longitude: Double, distance: Double) { // ~1 mile of lat a

Recommends Mike McDonald · 2017-10 · Stack Overflow · on “How to query closest GeoPoints in a collection in Firebase Cloud Firestore?”

We haven't exposed geoqueries yet, so currently there isn't a way to do this. Historically, the Firebase Realtime Database used geohashes [LINK] in a library called GeoFire [LINK] --but given that we plan to expose actual geoqueries in the near future, we haven't updated that library to Firestore. To learn how to do something similar yourself though, have a look at this video [this video] .

Recommends stparham · 2018-01 · Stack Overflow · on “How to run a geo "nearby" query with firestore?”

UPDATE: Firestore does not support actual GeoPoint queries at present so while the below query executes successfully, it only filters by latitude, not by longitude and thus will return many results that are not nearby. The best solution would be to use geohashes [LINK] . To learn how to do something similar yourself, have a look at this video [this video] . (First let me apologize for all the code in this post, I just wanted anyone reading this answer to have an easy time reproducing the functionality.) To address the same concern the OP had, at first I adapted the GeoFire library [LINK] to work with Firestore (you can learn a lot about geo-stuff by looking at that library). Then I realized I did

Recommends Michael Lehenbauer · 2018-11 · Stack Overflow · on “How to run a geo "nearby" query with firestore?”

Thanks for this answer! As other folks have mentioned, there is a large caveat though. Would you be willing to add something like this to the top? UPDATE: Firestore does not support actual GeoPoint queries at present so while the below query executes successfully, it only filters by latitude, not by longitude and thus will return many results that are not nearby. The best solution would be to use geohashes [LINK] . To learn how to do something similar yourself, have a look at this video [this video] .

Recommends Frank van Puffelen · 2019-02 · Stack Overflow · on “In order to sort geoquery results by distance must I read the entire dataset?”

Yes, in order to sort by distance you must read all results that fall into the Geoquery range. The reason for this is how such queries work: they return a set of documents that are within a range of geohash values, which is not necessarily the same order as by their distance to the center of the query. This also means that there is no way to do meaningful pagination in a list of documents that are ordered by their distance, since you need to read all results anyway. The best I can think of is implementing the Geoquery in Cloud Functions, so that you can do the sort/filter there, and only return the page-full of results to the client. While this doesn't save on your cost (as you're still read

Recommends Jay · 2019-04 · Stack Overflow · on “Google Firebase concerns and mixing database technologies in a web application”

The question should really include a specific description of the issue and the code you've attempted to write to solve it, otherwise it's borderline off topic. I find that linked article not very accurate; the query examples use a poorly formed structure, the info about location query is just not correct and statements like *incomplete string searches - this task is not possible* is not accurate as I do partial string queries *on a regular basis* in Firestore. Here's a great video with @FrankvanPuffelen on Geographic Location Query [this video]

Recommends Frank van Puffelen · 2019-05 · Stack Overflow · on “Ordering Firestore GeoHash query from closest to furthest?”

GeoFirestore uses geohashes to be able to select documents that are within a range of geohashes, which is roughly the same as an area on the map. There is no way to retrieve those documents within a specified order from within that area with just a geoquery. If you want to have the documents sorted by distance, you will have to do that after the geoquery, in your application code. If you want to learn more on why that is, have a look at a talk I gave a while ago: Querying Firebase and Firestore based on geographic location or distance [this video] . In it I explain how geohashes work, how they allow you to select documents in a certain geographic range, and why you can't do the more complex query

Recommends Alex Mamo · 2019-06 · Stack Overflow · on “firestore.where() on two fields at once”

According to the official documentation regarding query limitations [LINK] : Query limitations Cloud Firestore does not support the following types of queries: Queries with range filters on different fields, as described in the previous section. So as you can see, Cloud Firestore can only do a range filter on a single field and not on multiple fields as you intended to do. The most reasonable explanation is that Firestore cannot guarantee its performance in this case. Firestore must be able to return all results of a query in a single stream. To solve this, you'll have to query the database twice and combine the results of those queries client side. It's not perfect, since you need to query