Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query by key and order by date

Tags:

firebase

I have a database with a lot of "listings" structured as such:

Listings

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?

like image 946
Holger Sindbaek Avatar asked Mar 06 '15 23:03

Holger Sindbaek


People also ask

Can we use ORDER BY on date in SQL?

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.

How do I sort by date in SQL query?

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.).

How do you write a query for ORDER BY?

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.


1 Answers

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.

like image 172
Saeed D. Avatar answered Oct 21 '22 23:10

Saeed D.