我有一个我认为是对名为“电子邮件”的集合的简单查询:
const emailQuery = admin
.firestore()
.collection('email')
.where('sendAt', '>=', new Date())
.where('orderId', '==', doc.orderId)
.where('brandId', '==', doc.brandId);
我在这篇文章的主题中收到错误,表明我需要为此查询创建索引。我已经尝试为相关字段创建一对,但我不确定为什么需要它,因为只订购了一个字段——尽管有这些索引,错误仍然存在:
sendAt -> Ascending, brandId -> Ascending, orderId -> Ascending
sendAt -> Descending, brandId -> Ascending, orderId -> Ascending
如您所见,我没有指定 orderBy
。此外,sendAt
是一个日期,而其他两个是不需要排序的简单字符串。
我需要什么样的索引,或者我如何克服这个错误?
谢谢!
最佳答案
来自documentation :
If you attempt a compound query with a range clause that doesn't map to an existing index, you receive an error. The error message includes a direct link to create the missing index in the Firebase console.
换句话说:当您使用涉及 <
的比较时或 >
,在该字段的查询中有一个隐式排序。当与其他字段的条件结合使用时,特定集合 ID 需要索引。
您开始在问题标题中引用的错误消息应该包含一个指向控制台的链接,该控制台将为该查询创建所需的索引。只需点击该链接并授权创建索引。
关于database - Firebase Firestore 查询 : "Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51216614/