Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
755 views
in Technique[技术] by (71.8m points)

firebase - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...