1

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)

1 Answer 1

1

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

   
        }




 

  });});}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.