【问题标题】:How to Effectively query in Firebase?如何在 Firebase 中有效查询?
【发布时间】:2017-08-03 15:35:12
【问题描述】:

我在 Realtime DB 中有超过 2000 种产品。可以看到产品结构 下面

但问题是我需要使用 3 个 WHERE 子句进行查询。

我需要获取 CID = C09 && MID= S03 && SID= S03 的项目

我已经提到了下面的问题Query based on multiple where clauses in firebase

我已经研究了第一个解决方案。但是C09类有1000多个产品。我正在下载 1000 个产品以显示 20 个产品。

我尝试使用查询库。但它有一个issue

所以请给我一个有效的查询解决方案。

代码

var firstCat = firebase.database().ref('S01/Products').orderByChild("productCID").equalTo("C09");

  firstCat.once("value")
    .then(function (snapshot) {
      snapshot.forEach(function (childSnapshot) {
        var key = childSnapshot.key;

        var MID = childSnapshot.child("productMID").val();
        var SID = childSnapshot.child("productSID").val();

         if (MID == "M03" && SID == "S03") {
          var ProductID = childSnapshot.child("productID").val();
          var name = childSnapshot.child("productName").val();
          var unit = childSnapshot.child("productUnit").val();
          var status = childSnapshot.child("productStatus").val();
          var productMRP = childSnapshot.child("productMRP").val();
          var price = childSnapshot.child("productSellingPrice").val();
          var buying_price = childSnapshot.child("productBuyingPrice").val();

         }
      });
    });

【问题讨论】:

    标签: javascript firebase firebase-realtime-database nosql


    【解决方案1】:

    您似乎已经将必要的值编码到项目的键中。鉴于此,您可以使用密钥来查找匹配的子项:

    var ref = firebase.database().ref('S01/Products');
    var query = allProducts.orderByKey().startAt("C09M03S03").endAt("C09M03S04");
    query.on("value", function(snapshot) {
      snapshor.forEach(function(child) {
        console.log(child.key, child.val());
      });
    });
    

    【讨论】:

    • 如何查询“C09M03S09”,其中 S09 没有 endAt ID,因为它是我列表中的最后一个类别?
    • 只需一个 startAt()endAt("~")(它只是 ASCII 表中的一个字符,比您似乎使用的字符高)。
    猜你喜欢
    • 2017-12-21
    • 2020-08-14
    • 1970-01-01
    • 2020-10-26
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 2019-07-06
    相关资源
    最近更新 更多