database - Firebase Firestore 查询 : "Error: 9 FAILED_PRECONDITION: The query requires an index. You can create it here"

标签 database firebase indexing google-cloud-firestore

我有一个我认为是对名为“电子邮件”的集合的简单查询:

 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/

相关文章:

sql-server - 调整填充因子以减少碎片

database - Oracle SELECT - 双引号还是没有双引号?

sql - 与主键混淆

firebase - Firestore 标签数据模型最佳实践(数组与字典)

firebase - 如何在 Ionic 4 中使用 cordova firebase.dynamiclinks 插件?

postgresql - 这个 PostgreSQL 查询应该使用索引吗?

java - Realm.io [Java] 通知 - 如何仅监听特定表中的更改?

database - Zsh:找不到命令:mongo 在尝试使用 brew 安装 mongodb 4.2 之后

Firebase 自定义无密码登录电子邮件模板

parsing - 使用 ElasticSearch 索引和解析 XML 文件