Firestore orderBy查询未返回预期结果。

huangapple go评论50阅读模式
英文:

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.

Firestore orderBy查询未返回预期结果。

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).

huangapple
  • 本文由 发表于 2023年4月4日 09:11:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定