I have a database with a lot of "listings" structured as such:
I want to get 10 listings, starting or ending at a certain priority value, with a size value (child node) range between 9 and 10.5 and have the result ordered by date.
I've looked at an abundance of SO posts and Firebase examples, but I can't seem to find any solution.
Can it really be the case that this isn't possible in Firebase? It's quite a common use-case as far as I know.
Is there any solution to this? If not, how would you get around it?
ORDER BY is a clause in SQL which is used with SELECT query to fetch the records in ascending or descending order from a table. Just like we sort the integer and the string values stored in the column of the tables, similarly, we can sort the dates stored in the SQL table's column.
Use the ORDER BY keyword and the name of the column by which you want to sort. This way, you'll sort the data in ascending order by this column. You could also use the ASC keyword to make it clear that the order is ascending (the earliest date is shown first, the latest date is shown last, etc.).
Syntax. SELECT column-list FROM table_name [WHERE condition] [ORDER BY column1, column2, .. columnN] [ASC | DESC]; You can use more than one column in the ORDER BY clause.
You may want to review the Complex Queries in Firebase documentation.
Take a look at range queries. startAt()
and endAt()
allow you to look for shoes with specific sizes by setting starting and ending points for your queries.
For example, if we wanted to find all shoes that are between 9
and 10.5
, combine orderByChild()
, startAt
, and endAt
. Something like:
var ref = new Firebase(URL);
ref.orderByChild("timestamp").startAt("9").endAt("10.5").on("child_added", function(snapshot) {
console.log(snapshot.key());
});
If you could provide a code sample, then it would make it easier to pinpoint the solution. You may have to tweak your data model if you want to order your query by multiple keys.
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