我在实时数据库中有超过 2000 个产品。可以看到产品结构 下面
但问题是我需要使用 3 个 WHERE 子句进行查询。
I need to get a Item WHERE CID = C09 && MID= S03 && SID= S03
我提到了以下问题Query based on multiple where clauses in firebase
我已经研究了第一个解决方案。但C09类有1000多种产品。我正在下载 1000 个产品以显示 20 个产品。
我尝试使用 Querybase。但它有一个 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();
}
});
});
最佳答案
您似乎已经将必要的值编码到项目的键中。鉴于此,您可以使用键来查找匹配的子项:
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());
});
});
关于javascript - 如何在 Firebase 中有效查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43345592/