在我的 Firestore 数据库中,我的集合中有一些文档,如下所示:
{
name: "Item 1",
count: 2,
timestamp: January 29, 2018 at 3:43:12 PM UTC-8
}
我正在尝试查询这个集合,以便文档按以下顺序排序count
按降序排列,并且有他们的timestamp
的日期等于今天的日期。
Using Moment.js https://momentjs.com/,这是我到目前为止的查询:
const startOfToday = moment().startOf('day').toDate();
const endOfToday = moment().endOf('day').toDate();
const query = db.collection('items')
.orderBy('timestamp', 'desc')
.orderBy('count', 'desc')
.where('timestamp', '>=', startOfToday)
.where('timestamp', '<=', endOfToday);
这个查询确实获取了今天作为其的记录timestamp
的日期,但似乎是按timestamp
首先,如果两个有相同的timestamp
,然后它将按顺序排序count
.
显而易见的解决方案是切换顺序orderBy
函数,因此它会按以下顺序排序count
then timestamp
,但是当我尝试执行此操作时,Firestore 引发了以下错误:
FirebaseError:查询无效。您的 where 过滤器在字段“timestamp”上具有不等式( 或 >=),因此您还必须使用“timestamp”作为您的第一个 Query.orderBy(),但您的第一个 Query.orderBy( ) 位于字段“计数”上。
如果有人有任何想法,我将非常感激。