【问题标题】:How can I query Firebase for an equalTo boolean parameter?如何在 Firebase 中查询 equalTo 布尔参数?
【发布时间】:2015-12-17 01:25:15
【问题描述】:

我的数据库看起来像

{
  "chats" : {
    "-K5hUtxNOPLWwRTb8fAj" : {
      "clientId" : 893,
      "messages" : {
        "-K5hUtyCwWuPeFT0cSXr" : {
          "chatKey" : "-K5hUtxNOPLWwRTb8fAj",
          "messageText" : "new message",
          "timestamp" : 1450314801055,
          "userId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
        },
        "-K5hUvwKsAOvuvt-ymFG" : {
          "chatKey" : "-K5hUtxNOPLWwRTb8fAj",
          "messageText" : "did this work",
          "timestamp" : 1450314809127,
          "userId" : "48f7a208-5647-4d0e-921c-c49595de1db2"
        }
      },
      "newQuestion" : false,
      "startedByUserId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
    },
    "-K5hVKjs4SkSHD7WTh1I" : {
      "clientId" : 518,
      "messages" : {
        "-K5hVNzPKNgxLCgkfInI" : {
          "chatKey" : "-K5hVKjs4SkSHD7WTh1I",
          "messageText" : "this is new",
          "timestamp" : 1450314928109,
          "userId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
        }
      },
      "newQuestion" : true,
      "startedByUserId" : "19eaa3f0-4ab3-44b4-b418-03b64ca2313a"
    }
  }
}

如何获取所有将newQuestion 设置为truechats?我尝试了几个不同的查询,最新的是:

firebaseDb.child("chats/newQuestion").equalTo(true).once("value"...

但这抱怨布尔值需要订单?

【问题讨论】:

    标签: javascript json firebase


    【解决方案1】:

    你必须先指定一个orderBy 方法。

    var query = firebaseDb.child("chats").orderByChild("newQuestion").equalTo(true);
    

    Here's the Firebase docs on querying.

    【讨论】:

    • 如果我想按另一个字段排序所有聊天 - 比如clientId?但仅当newQuestiontrue 时?
    • 您需要更改数据结构以关闭 clientId。所以你可以做var query = firebaseDb.child("chats").child(clientId).orderByChild("newQuestion").equalTo(true);
    • 我将如何查询多个条件? clientId = 10 和 newQuestion = 真?
    • Firebase 仅支持一种 orderBy 方法。当查询变得更复杂时,在客户端进行排序会更容易。特别是如果记录集少于 1,000。
    猜你喜欢
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多