Reputation: 332
I have the following query
admin.firestore()
.collection("articles")
.orderBy("publishDate", "desc")
.where("publishDate", ">=", mintime)
.orderBy("views", "desc")
.limit(10);
The query gives the top 10 articles within a week ordered by their views I get no errors, but the resulting articles are not necessarily the articles with the highest view count. Small remark the 10 articles that are return are not sorted either, to fix this I just sorted the 10 articles after I got them from firebase, but it makes me think I'm doing something wrong. So is there anyway to fix this query to relably get the top articles within a date and ordered by views?
EDIT
I will try to explain it a little differently this query:
admin.firestore()
.collection("articles")
.orderBy("publishDate", "desc")
.where("publishDate", ">=", mintime)
.orderBy("views", "desc")
.limit(10);
And this query
admin.firestore()
.collection("articles")
.orderBy("publishDate", "desc")
.where("publishDate", ">=", mintime)
.limit(10);
returns the same result, it is like the .orderBy("views", "desc")
gets ignored, how can I fix this?
Upvotes: 0
Views: 40
Reputation: 317948
the 10 articles that are return are not sorted either, to fix this I just sorted the 10 articles after I got them from firebase
This is exactly what you have to do, and there are no workarounds for making Firestore do this for you, given the query you have now.
A secondary orderBy doesn't reorder the entire set of results again. It just sorts the documents within the values clustered by the primary sort value. This behavior is covered by an example in the documentation. Look for part where it says "You can also order by multiple fields".
Upvotes: 0