【问题标题】:Firestore - Filter by two array-contains queries in two different fieldsFirestore - 按两个不同字段中的两个数组包含查询进行过滤
【发布时间】:2023-03-07 12:28:01
【问题描述】:

我正在尝试使用 array-contains 过滤我的 Firestore 中的两个不同字段,我看到较早的帖子说您不能使用两个 array- contains 在同一个查询中,但想知道是否仍然如此,或者有其他方法可以达到相同的结果吗?

我确实认为,也许做两个单独的查询,但两个字段都在同一个集合中,所以不确定这是否是最佳方式。

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

    });

  });}

如果我结合使用 '!=' 和数组包含?

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

【问题讨论】:

    标签: javascript arrays google-cloud-firestore


    【解决方案1】:

    我有一个可能的解决方案。

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

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 2020-12-22
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 2015-10-20
      • 1970-01-01
      相关资源
      最近更新 更多