动态值的 Firebase indexOn 警告

Firebase indexOn warning for dynamic values

这是我的 Firebase 数据库结构和我的查询。

我对这个结构的查询是:

var messageRef = databaseRef.child(`/messagesThread/-KdzCvVW5icW7ZuSIAxV`);

messageRef.orderByChild(`/readBy/{userId}`).equalTo(false).on("child_added", (snapshots)=>{
  console.log("Unread messages = " + snapshots.numChildren());
});

但上面的查询给我警告

FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "readBy/{userId}".

那么如何用动态添加这个indexOn{userId}

由于通配符的索引不是很好,您可以修改结构然后索引:

---readBy-+
          |
           294jjfouurjkfJDGLS+
                             |
                            isRead: true

现在您可以添加安全规则:

"readBy": {
  ".indexOn": "isRead"
}