Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase query using . equalTo (boolean value, String key) not working

I’m trying to query my data to retrieve a list of items sorted alphabetically and filtered by those items marked as “visible”.

Here’s my data structure.

listItems
    list_1_uid
        item_1_uid
            name: "Item 01"
            nameLowercase: "item 01"
            visible: true
        item_2_uid
            name: "zItem 02"
            nameLowercase: "zitem 02"
            visible: false
        item_3_uid
            name: "aItem 03"
            nameLowercase: "aitem 03"
            visible: true

Here’s my query.

mQuery = mFirebaseDbReference
    .child("listItems")
    .child("list_1_uid")
    .orderByChild("nameLowercase")
    .equalTo(true, "visible");

My query returns no data. If I remove the .equalTo(true, "visible") statement the query returns my entire alphabetized list.

Any suggestions on how to retrieve my filtered list?

like image 860
Loren Avatar asked Feb 07 '23 20:02

Loren


1 Answers

Queries in Firebase are tied to property specified in the orderBy call. Therefore the equalTo(boolean value, String key) call in this example refers to nameLowercase, so it will query for all items with nameLowercase set to true. The second parameter refers to the key of the item (e.g. item_1_uid) and can be used to specify a starting point in the query. This is useful for paginating through the results for example.

Since there is no limit on this query, a solution would be to use a query that orders by "visible" and then do any additional sorting on nameLowercase client side with the final result:

mQuery = mFirebaseDbReference
    .child("listItems")
    .child("list_1_uid")
    .orderByChild("visible")
    .equalTo(true);
like image 130
jonnydee Avatar answered Feb 13 '23 07:02

jonnydee