Home > Mobile >  Firestore query on multiple fields
Firestore query on multiple fields

Time:02-02

I have a collection

created_at: 28 January 2023 at 14:41:04 UTC 5
id: "e44a154f-b0cb-4ef6-bc27-af37f178b46c"
message: "1 Orders has been uploaded successfully"
read: true
writer: "16315bd844e95c5926a92bda9" 

I want to write a query for getting all the records form the notifications collections which are orderBy created_at and where writer === "16315bd844e95c5926a92bda9"

I have tried this query

const ref = collection(db, "notifications")
query(
  ref, 
  where("writer", "==", '16315bd844e95c5926a92bda9'), 
  orderBy('created_at', 'desc'), 
  limit(5)
)

but it does not showing any data.

But when i delete the orderBy part its filtering the collection

CodePudding user response:

The query in your question needs a composite index, which (unlike single-field indexes) you need to add yourself.

If you (catch errors and) check the log output, you'll find an error message about a missing index, which included a link to the Firebase console where you can create the index with a single click.

  • Related