Javascript Firestore:多个条件where子句

Javascript Firestore:多个条件where子句,javascript,firebase,google-cloud-firestore,Javascript,Firebase,Google Cloud Firestore,例如,我为我的书籍列表设置了动态过滤器,可以在其中设置特定的颜色、作者和类别。 此过滤器可以一次设置多个颜色和多个类别 Book > Red, Blue > Adventure, Detective. 如何有条件地添加“where” firebase .firestore() .collection("book") .where("category", "==", ) .where("color", "==", ) .where("

例如,我为我的书籍列表设置了动态过滤器,可以在其中设置特定的颜色、作者和类别。 此过滤器可以一次设置多个颜色和多个类别

   Book > Red, Blue > Adventure, Detective.
如何有条件地添加“where”

  firebase
    .firestore()
    .collection("book")
    .where("category", "==", )
    .where("color", "==", )
    .where("author", "==", )

    .orderBy("date")
    .get()
    .then(querySnapshot => {...

正如您在API文档中看到的,该方法返回一个。CollectionReference扩展了。并返回查询对象。因此,您可以像这样重写代码:

var query = firebase.firestore().collection("book")
query = query.where(...)
query = query.where(...)
query = query.where(...)
query = query.orderBy(...)
query.get().then(...)
现在,您可以添加条件,以确定在每个阶段要应用哪些过滤器。只需使用每个新添加的筛选器重新分配
query

if (some_condition) {
    query = query.where(...)
}

除了@Doug Stevenson answer。当您有多个
,其中
时,有必要使其更具动态性,就像我的情况一样

function readDocuments(collection, options = {}) {
    let {where, orderBy, limit} = options;
    let query = firebase.firestore().collection(collection);

    if (where) {
        if (where[0] instanceof Array) {
            // It's an array of array
            for (let w of where) {
                query = query.where(...w);
            }
        } else {
            query = query.where(...where);
        }

    }

    if (orderBy) {
        query = query.orderBy(...orderBy);
    }

    if (limit) {
        query = query.limit(limit);
    }

    return query
            .get()
            .then()
            .catch()
    }

// Usage
// Multiple where
let options = {where: [["category", "==", "someCategory"], ["color", "==", "red"], ["author", "==", "Sam"]], orderBy: ["date", "desc"]};

//OR
// A single where
let options = {where: ["category", "==", "someCategory"]};

let documents = readDocuments("books", options);

例如,有这样一个数组

const conditionList = [
  {
    key: 'anyField',
    operator: '==',
    value: 'any value',
  },
  {
    key: 'anyField',
    operator: '>',
    value: 'any value',
  },
  {
    key: 'anyField',
    operator: '<',
    value: 'any value',
  },
  {
    key: 'anyField',
    operator: '==',
    value: 'any value',
  },
  {
    key: 'anyField',
    operator: '==',
    value: 'any value',
  },
]

您将获得集合集查询的条件。

这对我非常有用!非常感谢。编辑:它起作用了,当你重新分配
query=
时,我错过了…–这对我不管用。所有where调用都被忽略。这对你仍然有效吗?@JimmyKane什么是
afs
?你在用AngularFire吗?如果是,则所有对象都不同。AF将所有内容都封装到自己的类型中。应该注意的是,虽然您可以使用不同属性按多个where进行查询,但仍然无法查询同一属性的多个where。以您的问题为例,查询
仍然没有解决方案。where(“category”,“category”,“explore”)。where(“category”,“category”,“detective”)
@pikilon这只是自以为是的语法。Typescript应该允许您重新定义变量,使用
let
来定义它们。我知道这是不推荐的,因为它可能由于范围问题而导致意外行为。如果您只是想避免警告,那么
@ts ignore
就可以了。我想到的另一件事是将每个新结果推送到一个数组中:
const querys=[firebase.firestore().collection(“book”);push(查询[querys.length-1]。其中(…);查询[querys.length-1].get().then(…)
。或者您可以编写一个包装器类。。。选择你的毒药;-)
async yourFunction(){
    const Ref0 = firebase.firestore().collection("your_collection").doc(doc.id)

    const Ref1 = appointmentsRef.where('val1', '==',condition1).get();
    const Ref2 = appointmentsRef.where("val2", "!=", condition2).get()

    const [snapshot_val1, snapshot_val2] = await Promise.all([Ref1, Ref2]);

    
    const val1_Array = snapshot_val1.docs;
    const val2_Array = snapshot_val2.docs;

    const globale_val_Array = val1_Array .concat(val2_Array );

    return globale_val_Array ;
  }



/*Call you function*/
this.checkCurrentAppointment().then(docSnapshot=> {
      docSnapshot.forEach(doc=> {
          console.log("Your data with multiple code query:", doc.data());
      });
    });
async yourFunction(){
    const Ref0 = firebase.firestore().collection("your_collection").doc(doc.id)

    const Ref1 = appointmentsRef.where('val1', '==',condition1).get();
    const Ref2 = appointmentsRef.where("val2", "!=", condition2).get()

    const [snapshot_val1, snapshot_val2] = await Promise.all([Ref1, Ref2]);

    
    const val1_Array = snapshot_val1.docs;
    const val2_Array = snapshot_val2.docs;

    const globale_val_Array = val1_Array .concat(val2_Array );

    return globale_val_Array ;
  }



/*Call you function*/
this.checkCurrentAppointment().then(docSnapshot=> {
      docSnapshot.forEach(doc=> {
          console.log("Your data with multiple code query:", doc.data());
      });
    });