在 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
我应该在“收到的消息”集合以及“messageSeen”和“日期”字段上放置索引以加快查询速度吗?
最佳答案
当我尝试使用两个 orderBy() 子句运行查询时,出现错误:
The query requires an index. You can create it here: ...
添加错误消息中链接的索引后,我可以检索按状态然后按索引排序的文档。在这里查看我的工作 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);
})
})
关于javascript - 在 Firebase Firestore 中,我想使用 orderBy 两次。是否需要创建索引来加快查询速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50011775/