0

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

1
  • The query in your question needs a composite index, which you need to add manually. 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. Commented Jan 31, 2023 at 14:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I happened to create index at firebase where I set the created_at desc, but when i am querying by asc its giving me error. Is there a way to query by both order i.e. ace and desc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.