代码之家  ›  专栏  ›  技术社区  ›  Jeff Padgett

在Firebase Firestore中,我想使用orderBy两次。我是否需要创建索引来加速查询?

  •  0
  • Jeff Padgett  · 技术社区  · 7 年前

    在Firebase Firestore中,我想使用orderBy两次。我是否需要创建索引来加速查询?

    例如:

    Query query = fsDB.collection("users").document(currentUID).collection("received_messages")
    .orderBy("messageSeen").orderBy("date");
    

    当使用范围或“where”时,不会显示自动错误消息。

    结构如下所示:

    received_messages
    
      date:  01/02/99
    
      messageSeen:  true
    
      from:  keuajopdf315 
    

    我是否应该在集合“received messages”以及“messageSeen”和“date”字段上设置索引以加快查询速度?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Frank van Puffelen    7 年前

    当我尝试使用两个orderBy()子句运行查询时,出现了一个错误:

    查询需要索引。您可以在此处创建:。。。

    添加错误消息中链接的索引后,我可以检索按状态排序的文档,然后再按索引排序。在此处查看我的工作jsbin: https://jsbin.com/rewujav/edit?js,console

    docs.orderBy("state").orderBy("index").get().then(function(snapshot) {
      snapshot.forEach((doc) => {
        console.log(doc.id+": state="+doc.data().state+" index="+doc.data().index);
      })
    })