英文:
Firestore orderBy query not returning expected results
问题
I'll provide a translation of the code and the issue you're facing:
尝试返回带有多个“where”过滤器的orderBy查询时,我没有收到预期的结果。
在上面的代码中,我期望首先获得最高的“total”字段值。但我得到的是一个随机的顺序。
最终,我只想显示前1个结果(在车展中评分)。您可以在图像中看到HSV VAN应该是排名最高的,但它在列表中的位置很低。
我期望在应用了limit(1)的查询中,具有最高“total”字段值的文档会被返回。
Please note that the code part is not translated, as per your request. If you have any specific questions or need further assistance, feel free to ask.
英文:
When returning an orderBy query with multiple 'where' filters, i'm not receiving the expected results.
try {
const mvResults = this.afs.collection(`leader_board`, ref => ref
.where('year', '>=', '1990')
.where('event', '==', 'WAVUST')
.where('body', 'in', ['Panelvan', 'SM Van', 'XL Van'])
.orderBy('year')
.orderBy('total', 'desc')
.limit(10)).valueChanges()
this.topModVan$ = mvResults.subscribe(data => {
this.topModernVanDS = new MatTableDataSource(data);
console.log('Top Modern Van: ' + JSON.stringify(data));
})
} catch (error) {
console.log('Top Modern Van Error: ' + error.message);
}
In the above code, I expect to get the highest returned 'total' field value first. What I am getting is a random pattern.
Ultimately I only want to show the top 1 result (judging at a car show). You can see in the image HSVVAN should be the top, but it is way down the list.
I am expecting the document with the highest 'total' field value would be return in a query with limit(1) is applied.
答案1
得分: 1
你的查询首先按year
排序,然后再按total
排序,这意味着只有在year
具有相同值的文档中才会考虑total
的值。
不幸的是,无法交换排序子句,因为需要首先使用year
,这是因为.where('year', '>=', '1990')
条件。
唯一的解决方案是在应用程序代码中根据total
对结果进行重新排序(首先或唯一)。
英文:
Your query is first ordered by year
and only then by total
, which means that the total
value is only taken into consideration for documents where year
has the same value.
Unfortunately there's no way to swap the ordering clauses, as you need year
first because of the .where('year', '>=', '1990')
condition.
The only solution is to reorder the results in your application code on total
(first, or only).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论