Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order by with where inequality with limit on Firestore

Assuming that I have got the following simple query with where and orderBy and limit, that will return me public profile (hence where), that has been recently updated (hence orderBy).

db.collection('profile')
   .where('public_profile', '==', true)
   .orderBy('updated_at', 'desc')
   .limit(1)
   .onSnapshot(function(doc) {
        ...
   });

Now, I want to add some additional logic, meaning I want to only display profiles who have are below some kind of level. When I do the following:

db.collection('profile')
   .where('public_profile', '==', true)
   .orderBy('updated_at', 'desc')
   .where('level', '<=', 5)
   .limit(1)
   .onSnapshot(function(doc) {
        ...
   });

I get the error of:

Invalid query. You have a where filter with an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) on field 'talent.level' and so you must also use 'level' as your first queryOrderedBy field, but your first queryOrderedBy is currently on field 'updated_at' instead.

As I understand, I need to add additional orderBy, giving me the following:

db.collection('profile')
   .where('public_profile', '==', true)
   .where('level', '<=', 5)
   .orderBy('level', 'desc')
   .orderBy('updated_at', 'desc')
   .limit(1)
   .onSnapshot(function(doc) {
        ...
   });

However, this yields me with the wrong result: it doesn't return the most recently updated public profile, whose level is lower or equal to 5. It returns me the the the most recently updated public profile, of the profile with the highest level.

Has any of you found a similar issue? If yes, what was your solution?

like image 411
uksz Avatar asked May 16 '19 12:05

uksz


People also ask

How do I query multiple values in firestore?

With the in query, you can query a specific field for multiple values (up to 10) in a single query. You do this by passing a list containing all the values you want to search for, and Cloud Firestore will match any document whose field equals one of those values.

How do I sort my firestore database?

You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field. The result set will not include documents that do not contain the given field.

How do I aggregate data in firestore?

If you want to gain insight into properties of the collection as a whole, you will need aggregation over a collection. Cloud Firestore does not support native aggregation queries. However, you can use client-side transactions or Cloud Functions to easily maintain aggregate information about your data.

What is the use of FireStore query?

Firestore provides powerful query functionality for specifying which documents you want to retrieve from a collection. These queries can also be used with either get () or addSnapshotListener (), as described in Get Data. Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.

How do I limit the number of documents in a FireStore?

Firestore queries can use orderBy () to sort documents and limit () to limit the number of documents retrieved. All queries must be backed by an index, but simple indexes are automatically created.

What is orderby in FireStore query?

Note that request.query.orderBy is a map of field names to sort direction, and the direction is always capitalized. (I couldn’t find this anywhere in the documentation!) Firestore queries can use orderBy () to sort documents and limit () to limit the number of documents retrieved.

What are the limitations of ordering data in a range comparison?

There is only one not-so-obvious limitation concerning ordering data – when you’re using a range comparison (<, <=, >, >=), you can order data only by the parameter you’re querying with. So in our case, when we fetched model released after 2021, we could only order them by release date (i.e., ascending order).


1 Answers

As the documentations states in the limitation page (click) if you use a range comparison (<, <=, >, >=) the first order by that you use should be the same field filtered.

After fixing the order it will probably prompt you to create ad index of that collection in order to use those conditions.

Tip: On firestore the where condition should be at the top, and order by should be at the bottom. (it's useful to better understand the condition orders)

like image 137
Kerlos Bekhit Avatar answered Nov 15 '22 01:11

Kerlos Bekhit