【问题标题】:Firebase index required but no link provided需要 Firebase 索引,但未提供链接
【发布时间】:2021-07-15 05:03:19
【问题描述】:

提示:不要在此处创建索引,而是在您的代码中运行查询 - 如果您缺少任何索引,您将看到创建它们的链接错误。 Learn more

我收到错误no matching index found,没有创建所需索引的常用直接链接。

我的数据库结构如下:

answers (collection)
  - $docId (document)
      - $refId (collection)
          - $docId (document)
              - created_at (timestamp)
              - type (string)

我的查询是:

firebase.firestore()
 
 .collection('answers')
 .doc('abcdHajM930Gabcd')
 .collection('abcdetIo25xYqAabcd')
 .orderBy("created_at", "desc")
 .limit(5)
 .where("type", "==", "3")

我已经尝试了所有我能想到的索引组合(下面的屏幕截图),我做错了什么?

collection field type
answers created_at: DESC type: ARRAY
answers created_at: DESC type: ARRAY
answers created_at: DESC type: ASC
answers created_at: DESC type: ASC
answers type: ASC created_at: DESC
answers type: ASC created_at: DESC

【问题讨论】:

    标签: firebase google-cloud-firestore


    【解决方案1】:

    更新:issue 似乎已修复,Javascript 客户端 SDK 现在返回索引创建链接。


    我不完全确定 Firebase JS SDK 是否有意这样做,但如果您尝试在 Cloud Function 中运行查询(只需使用模拟器),那么它应该会引发包含创建索引的链接的错误.

    export const getIndexLink = functions.https.onRequest(async (request, response) => {
        //   functions.logger.info("Hello logs!", {structuredData: true});
        try {
            const snap = await admin.firestore().collection('answers')
                .doc('abcdHajM930Gabcd')
                .collection('abcdetIo25xYqAabcd')
                .where("type", "==", "3")
                .orderBy("created_at", "desc")
                .limit(5).get()
            console.log(snap.size)
            response.send("Hello from Firebase!" + snap.size);
        } catch (error) {
            console.log(error)
            response.send(error.message)
        }
    });
    

    我尝试复制您的查询并使用该 URL 创建一个索引,结果该索引是:

    您正在使用的字段不是“答案”集合文档的一部分。现在,如果您更改子集合名称,它将需要另一个索引。您的 Firestore 结构是什么样的?我建议对其进行一些重组并获得相同的子集合名称并创建一个具有 collectionGroup 范围的索引。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    相关资源
    最近更新 更多