1

How do I get the highest score from within a date range in a single request on firebase firestore?

await Firestore.collection(CollectionName.post)
                    .orderBy("score")
                    .where("publishedAt", ">", Firebase.firestore.Timestamp.fromDate(GetDateAndMinus(24)))
                    .where("publishedAt", "<=", Firebase.firestore.Timestamp.now())
                    .get(),
            });

Invalid query. You have a where filter with an inequality (<, <=, >, or >=) on field 'publishedAt' and so you must also use 'publishedAt' as your first orderBy(), but your first orderBy() is on field 'score' instead.

phongyewtong
  • 5,085
  • 13
  • 56
  • 81
  • What's wrong with the orderBy that you have commented out here? Please edit the question to explain what isn't working the way you expect. Be sure to check for errors on the query and share the error message if there is one. – Doug Stevenson Nov 15 '20 at 16:48
  • you can only use 1 field. you cant use score and published data together. i wonder if there is a work around. – phongyewtong Nov 15 '20 at 16:53
  • Please edit the question to show the specific error that you're getting. Right now, we can't see that you're doing any error handling at all. – Doug Stevenson Nov 15 '20 at 16:58
  • The workaround is to sort the documents by score in your app code. Firestore will not be able to do this for you on its backend. – Doug Stevenson Nov 15 '20 at 17:45

0 Answers0