LyonsTheWay2Gold
LyonsTheWay2Gold

Reputation: 373

Firestore - Filter by two array-contains queries in two different fields

I am trying to filter two different fields in my Firestore using array-contains, I have seen older posts saying you can't use two array-contains in the same query but was wondering if this is still the case, or is there another way I could achieve the same outcome?

I did think, maybe do two separate queries, but both fields are in the same collection, so not sure if this would be the optimum way.

function getAllUserPlumb(){

      var engineercounty = $('#engcounty').val();

    console.log(engineercounty);

    var docRef = db.collection("users");
    docRef.where("trade", "array-contains", "Plumbing").where("engpostcode", "array-contains", engineercounty).get().then(function(querySnapshot) {
    querySnapshot.forEach(function(doc) {
         // console.log(doc.data());
      html = createEngineerRow(doc.data(),doc.id);

       // $('#user_id').find('tbody').append(html);

    });

  });}

Maybe if I used '!=' in combination with array-contains?

docRef.where("trade", '!=', ['Drainage', 'Gas & Heating', 'Glazing', 'Locksmiths', 'Pest Control', 'Electrical']).where("engpostcode", "array-contains", engineercounty)

Upvotes: 1

Views: 54

Answers (1)

LyonsTheWay2Gold
LyonsTheWay2Gold

Reputation: 373

I have a possible solution.

function getAllUserPlumb(){

      var engineercounty = $('#engcounty').val();

    // console.log(engineercounty);

    var docRef = db.collection("users");
    docRef.where("engpostcode", "array-contains", engineercounty).get().then(function(querySnapshot) {


      querySnapshot.forEach(function(doc) {


        if(doc.data().trade.includes('Plumbing') ){
  
           
         // console.log(doc.data());
          html = createEngineerRow(doc.data(),doc.id);

       // $('#user_id').find('tbody').append(html);

   
        }




 

  });});}

Upvotes: 1

Related Questions