2017-04-11 20 views
0

我有超过2000个实时数据库产品。你可以看到,产品结构 下面如何在Firebase中有效查询?

enter image description here

但问题是,我需要用3个WHERE子句来查询。

我需要一个项目WHERE CID = C09 & & MID = S03 & & SID = S03

我也提到了以下问题Query based on multiple where clauses in firebase

我已经在第一解决方案工作。但C09类产品已超过1000种。我正在下载1000个产品以显示20个产品。

我试过使用Querybase。但它有一个issue

所以请建议我一个有效的解决方案来有效地查询。

CODE

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(); 

     } 
     }); 
    }); 

回答

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()); 
    }); 
}); 
+0

我如何查询“C09M03S09”,其中S09没有ENDAT ID,因为它是在我的列表中的最后一类? – user3467240

+0

只是'startAt()'会这样做,或者'endAt(“〜”)'(它只是ASCII表中比你使用的字符更高的一个字符)。 –