A
A
AndreyRafalsky132019-02-18 22:37:35
Android
AndreyRafalsky13, 2019-02-18 22:37:35

What is the correct way to use whereGreaterThan(), orderBy() and startAt() in a query (Firebase)?

Please tell me how to make a request. The collection contains note objects with title, description, priority fields. I need to get only those with:

  1. Priority greater than 1
  2. Title starts with "A"

Both conditions must be met. Wrote this query:
collectionReference
                .whereGreaterThan(KEY_PRIORITY, 1)
                .orderBy(KEY_TITLE)
                .orderBy(KEY_PRIORITY)
                .startAt("A")

Got an exception:

java.lang.IllegalArgumentException: Invalid query. You have an inequality where filter (whereLessThan(), whereGreaterThan(), etc.) on field 'priority' and so you must also have 'priority' as your first orderBy() field, but your first orderBy() is currently on field 'title' instead.

I don’t understand how to fix the error and write the correct implementation to get data for these two conditions. Help me to understand. Thanks in advance for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
luna3956, 2019-02-18
@AndreyRafalsky13

Errors:
1 - sorting must always precede other operations in the record, i.e.

ref.OrderBy(KEY_PRIORITY).whereGreaterThan(key_priority, 1)

2 - startAt does not define the first letter of the field.
There are two solutions - either you pull all records with a priority higher than 1 (that is, just
collectionReference.whereGreaterThan(KEY_PRIORITY, 1)
) and already on the client you filter out everything that does not start with "A", or enter an additional field, for example, firstLetter and put the first letter there for each entry. In this case, the request would look something like this:
UPD: when you try, give feedback whether it worked or not)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question