Firebase filter by field and sort by another field

Viewed 3672

I have a database in Firebase with the following data:

{
    user: 1;
    points: 100;
},
{
    user: 1;
    points: 510;
},
{
    user: 2;
    points: 110;
},
{
    user: 1;
    points: 85;
},
{
    user: 3;
    points: 300;
},
{
    user: 1;
    points: 500;
}

How can I make a query so that it only shows the fields of user 1 sorted from highest to lowest with REST queries?

I have tried with

https://xxxxxx.firebaseio.com/tirada.json?orderBy="user"&equalTo="1"

And it returns filtered by User but without sorting by Points

    {
        user: 1;
        points: 100;
    },
    {
        user: 1;
        points: 510;
    },
    {
        user: 1;
        points: 85;
    },
    {
        user: 1;
        points: 500;
    }

I need this result

{
        user: 1;
        points: 85;
    },
    {
        user: 1;
        points: 100;
    },
    {
        user: 1;
        points: 500;
    },
    {
        user: 1;
        points: 510;
    }

Thank's

1 Answers

Firebase Database queries can only order/filter on a single property. In many cases it is possible to combine the values you want to filter on into a single (synthetic) property. In your case that would translate to a property "user_points": "1_510" that you can then order/filter on. But the REST API returns raw JSON objects, and the order of properties in a JSON object is undefined. So this means that you'll have to sort the results on the point value in your client-side code anyway. Given that, you might as well stick to only filtering on user anyway.

Also see:

Related