Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore compound query with <= & >=

I need to query a firestore collection for entries where a start time (which is a number) is >= slot.start and and <= slot.end.

like this:

.collection('Entries', ref => ref.where('timeStart', '>=', slot.timeStart).where('timeEnd', '<=', slot.timeEnd))

but I get this error:

Invalid query. All where filters with an inequality (<, <=, >, or >=) must be on the same field.

I thought this query would work with composite index but as the error says that does not work.

When I change the query to this:

.collection('Entries', ref => ref.where('timeStart', '==', slot.timeStart).where('timeEnd', '<=', slot.timeEnd))

I can create an index for it. Furthermore when I change the 2nd '<=' to an '==' aswell I don't even need an index for it.

I didn't think my query is that advanced and I'm a little bit disappointed that firestore can't do this query. So what is the best solution for my problem? Is the next best thing to omit the 2nd query statement like this

.collection('Entries', ref => ref.where('timeStart', '>=', slot.timeStart))

And then iterate through the results and check if timeEnd <= entry.timeEnd "manually"? That seems like a really bad solution for me because I may need to load a lot of data then.

like image 805
Jonas Avatar asked Aug 28 '18 20:08

Jonas


1 Answers

You're going to have to pick one field and query that one then filter out the other on the client side. Compound Queries documentation is firm on this one.

You can only perform range comparisons (<, <=, >, >=) on a single field, and you can include at most one array_contains clause in a compound query:

like image 188
Ronnie Royston Avatar answered Oct 13 '22 11:10

Ronnie Royston