0

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)

clark
  • 101
  • 9
  • Trying to map SQL queries directly to a NoSQL database like Firebase is a recipe for problems. I recommend reevaluating your use-case after reading [NoSQL data modeling](https://highlyscalable.wordpress.com/2012/03/01/nosql-data-modeling-techniques/) and watching [Firebase for SQL developers](https://www.youtube.com/playlist?list=PLl-K7zZEsYLlP-k-RKFa7RyNPa9_wCH2s). – Frank van Puffelen Oct 23 '17 at 01:03

1 Answers1

1

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.

Andrew Powers
  • 126
  • 2
  • 4