【发布时间】:2019-05-16 08:11:40
【问题描述】:
我有一个带有相等运算符的where() 方法和一个orderBy() 方法的查询,但我不知道为什么它需要索引。
where 方法检查对象(地图)中的值,并且使用数字进行排序。
文档说
如果您有一个具有范围比较(、>=)的过滤器,则您的第一个排序必须在同一字段中
所以我认为平等过滤器会很好。
这是我的查询代码:
this.afs.collection('posts').ref
.where('tags.' + this.courseID,'==',true)
.orderBy("votes")
.limit(5)
.get().then(snap => {
snap.forEach(doc => {
console.log(doc.data());
});
});
【问题讨论】:
-
正如 Alex 回答的:Firestore 自动为每个单独的字段创建索引。但是您的查询需要
'tags.' + this.courseID和votes上的复合索引,这不是自动创建的。请注意,您可以通过using an array and thearray-containsoperator 简化您的查询。除了更简单之外,这可能不需要额外的索引。 -
@FrankvanPuffelen 很棒的想法。
array-contains确实需要一个索引,但只有一个,而我需要一个新的索引来为每个 courseID 设置一个相等的索引。谢谢!
标签: javascript firebase google-cloud-firestore angularfire2