简体   繁体   English

Angular Firestore集合查询和排序依据

[英]Angular Firestore Collection Query & Order By

I am performign a simple query and then want to order by date. 我正在执行一个简单的查询,然后要按日期排序。

This works perfectly fine: 这工作得很好:

this.afs.collection('orders', ref=> ref.where('orderStatus', '<', 7));

This throws an error when I want to filter orderStaus < 7 and then order it by date. 当我想过滤orderStaus <7然后按日期排序时,这将引发错误。

this.afs.collection('orders', ref=> ref.where('orderStatus', '<', 7).orderBy('date', 'desc'));

Here is my error: 这是我的错误:

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

How can I achieve getting all ordersStatus < 7 AND then sort the orders by date? 如何获得所有的订单状态<7,然后按日期对订单进行排序?

Cloud Firestore requires you to first order by the field on which you are applying the range filter on and then order by any other field of your choice. Cloud Firestore要求您首先根据要应用范围过滤器的字段进行排序,然后再根据您选择的任何其他字段进行排序。

Reference : https://firebase.google.com/docs/firestore/query-data/order-limit-data 参考https : //firebase.google.com/docs/firestore/query-data/order-limit-data

You can achieve this by creating a compound index of range filter field and the field you want to order next for the collection you are querying. 您可以通过创建范围过滤器字段的复合索引以及要查询的集合的下一个要排序的字段来实现此目的。

So in your case you need to create a compound index for the orders collection with orderStatus DESC and date DESC . 因此,在您的情况下,您需要使用orderStatus DESCdate DESCorders集合创建一个复合索引。

Reference : https://firebase.google.com/docs/firestore/query-data/indexing 参考https : //firebase.google.com/docs/firestore/query-data/indexing

Hope this is useful. 希望这是有用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM