1

Using firestore v9 I faced up with 1 problem. I have a list of documents, and here 1 of them:

end_hour: 21 master: "WIZ1d74QmvMNDEQKulJyiQNCnad2" start_hour: 13 working_day: January 11, 2022 at 9:00:00 PM UTC+3

And I try to receive all documents, where master is equal as passed ID and working_day more than passed Date.

const response = await db.collection('schedule')
        .where('working_day', '>=', new Date())
        .where('master', '==', masterId)
        .get();

This request doesn't return any data at all, but when I do request using only first

const response = await db.collection('schedule')
        .where('working_day', '>=', new Date()).get()

or when I do query by second field

    const response = await db.collection('schedule')
        .where('master', '==', masterId)
        .get();

Both of them works alone, but not in a pair and I just don't know what is the problem

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84
  • You should store 'working_day' as a number – Jonathan Jan 10 '22 at 18:39
  • 2
    Compounded queries in Firestore (like you are trying to do) will usually throw an error with a link to create an index of the query you are requesting. Pretty sure this is still valid for Firestore V9. You click on the link and Cloud Firestore will take care of the rest, check the error you are getting and if this is the case. [More on Firestore indexes](https://firebase.google.com/docs/firestore/query-data/indexing) – Caio Mar Jan 10 '22 at 18:47

1 Answers1

0

As mentioned by Caio, you'll need a composite index. From https://cloud.google.com/firestore/docs/query-data/queries#compound_queries, it says "You can chain multiple equality operators (== or array-contains) methods to create more specific queries (logical AND). However, you must create a composite index to combine equality operators with the inequality operators, <, <=, >, and !=."

Jim Morrison
  • 2,784
  • 1
  • 7
  • 11
  • I added the indexes and after that checked using: **firebase firestore:indexes** `{ "collectionGroup": "schedule", "queryScope": "COLLECTION", "fields": [ { "fieldPath": "working_day", "order": "DESCENDING" }, { "fieldPath": "master", "order": "ASCENDING" } ] }` And I could see that Indexes has been applied, but request still without the answer. Also I don't see any error in console – Zakhar Krauchuk Jan 11 '22 at 07:35
  • I couldn't find it in the docs, but you should have your order field last in the index. I.e. fields: [master, working_day]. Otherwise, you can't order by working_day based on this index if you filter on master. Was this the index suggested by your previous query? – Jim Morrison Jan 12 '22 at 16:29
  • yeah, I've tried several variants of query. Each one return nothing, just long request without any response. I didn't even see the firebase log about indexing for applying – Zakhar Krauchuk Jan 12 '22 at 16:52