Is there a fast way to perform database-style queries with Firebase?
For example:
Given a firebase reference users
with fields user_id
, name
, and age
, what would be the best way to do a query similar to this:
SELECT name FROM users WHERE `user_id`=147;
and
SELECT COUNT(*) FROM users WHERE age=21;
If your data model needs something more than a single join query, never choose firebase. It is highly suggested not to choose Firebase for android application if your intended operation is to perform deep and complex querying.
It is not possible to use Firebase in this way. Firebase is a real-time object store. It is not a SQL database and is not intended to be a replacement for one. It completely lacks mechanisms such as JOINs, WHERE query filters, foreign keys, and other tools relational databases all provide.
Firebase security rules, this video does an amazing job explaining them, are a way in which you can sort of create schemas. An important consideration when using these rules is that if you're using the Firestore Admin SDK (used for backend code) then your rules will be bypassed.
In general, no. Firebase is essentially a "realtime database", constantly streaming updates to you as data changes, so it's more difficult to do general-purpose querying. For now, there are a couple of (admittedly limited) querying primitives that are provided. See the Queries/Limits page in the docs.
You can often work around these limitations through a variety of approaches:
We intend to improve on this over time, as we realize it's a weak spot compared to the flexible querying provided by traditional relational database systems.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With