0

I have over 2000 Products in Realtime DB. You can see the Product structure below

enter image description here

But the Problem is that I need to query with 3 WHERE clauses.

I need to get a Item WHERE CID = C09 && MID= S03 && SID= S03

I have referred the below question Query based on multiple where clauses in firebase

I have worked on the first solution. But Category C09 has over 1000 products. I am downloading 1000 products to show 20 products.

I tried using Querybase. But it has an issue.

So please suggest me an effective solution to query effectively.

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

         }
      });
    });
Community
  • 1
  • 1
Arulnadhan
  • 923
  • 4
  • 17
  • 46

1 Answers1

1

You seem to already be encoding the necessary values into the key of the items. Given that, you can use the key to find the matching children:

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());
  });
});
Arulnadhan
  • 923
  • 4
  • 17
  • 46
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807