clark
clark

Reputation: 101

Firebase Joining, Grouping, Aggregation Equivalent in SQL

I am coming from SQL and trying Firebase for the fist time. Is this query possible with Firebase?

SELECT
    name,
    AVG(rating) as avgRating
FROM restaurants
JOIN reviews
    ON restaurants.ID = reviews.restaurantsID
GROUP BY restaurants.ID
ORDER BY avgRating;

If yes, can you provide me a link to a blog, tutorial or an advanced course on Firebase for SQL guys? (preferably Firebase for Web)

Upvotes: 0

Views: 886

Answers (1)

Andrew Powers
Andrew Powers

Reputation: 136

Joins are the primary weak point for all NoSQL style databases. This arises from the way they work: a query is just a straight read of a JSON store. (Which also gives them their strength: speed and scalability.)

If you are using Firebase real-time DB, then the best approach to this is to denormalize your data and perform two queries. That would look something like this:

firebase.db.ref("/path/to/ref").on("value", (snapshot) => {
  if(snapshot.val()){
    firebase.db.ref(`/second/ref/${snapshot.val().joinValue}`).on("value", (snapshot2) => {
      console.log(snapshot2.val()) // Do something
    }
  }
})

Recently, Firebase released their new FireStore database that is implemented to address some of the weak points with the real-time version. However, I don't think the join problem is specifically addressed (the end solution is probably similar to my example.)

Finally, I work with Firebase quite a bit and I suspect they are probably working with a GraphQL inspired solution which would be a full interface for a SQL based DB. I would expect that to drop in the next 12 months.

Upvotes: 1

Related Questions