Firestore: Multiple conditional where clauses

Javascriptnode.jsFirebaseGoogle Cloud-PlatformGoogle Cloud-Firestore

Javascript Problem Overview


For example I have dynamic filter for my list of books where I can set specific color, authors and categories. This filter can set multiple colors at once and multiple categories.

   Book > Red, Blue > Adventure, Detective.

How can I add "where" conditionally?

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

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

Javascript Solutions


Solution 1 - Javascript

As you can see in the API docs, the collection() method returns a CollectionReference. CollectionReference extends Query, and Query objects are immutable. Query.where() and Query.orderBy() return new Query objects that add operations on top of the original Query (which remains unmodified). You will have to write code to remember these new Query objects so you can continue to chain calls with them. So, you can rewrite your code like this:

var query = firebase.firestore().collection("book")
query = query.where(...)
query = query.where(...)
query = query.where(...)
query = query.orderBy(...)
query.get().then(...)

Now you can put in conditionals to figure out which filters you want to apply at each stage. Just reassign query with each newly added filter.

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

Solution 2 - Javascript

Firebase Version 9

The docs do not cover this but here is how to add conditional where clauses to a query

import { collection, query, where } from 'firebase/firestore'

const queryConstraints = []
if (group != null) queryConstraints.push(where('group', '==', group))
if (pro != null) queryConstraints.push(where('pro', '==', pro))
const q = query(collection(db, 'videos'), ...queryConstraints)

The source of this answer is a bit of intuitive guesswork and help from my best friend J-E^S^-U-S

Solution 3 - Javascript

With Firebase Version 9 (Jan, 2022 Update):

You can filter data with multiple where clauses:

import { query, collection, where, getDocs } from "firebase/firestore";

const q = query(
  collection(db, "products"),
  where("category", "==", "Computer"),
  where("types", "array-contains", ['Laptop', 'Lenovo', 'Intel']),
  where("price", "<=", 1000),
);

const docsSnap = await getDocs(q);
    
docsSnap.forEach((doc) => {
  console.log(doc.data());
});

Solution 4 - Javascript

In addition to @Doug Stevenson answer. When you have more than one where it is necessary to make it more dynamic as in my case.

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

Solution 5 - Javascript

If you're using angular fire, you can just use reduce like so:

const students = [studentID, studentID2,...];

this.afs.collection('classes',
  (ref: any) => students.reduce(
    (r: any, student: any) => r.where(`students.${student}`, '==', true)
    , ref)
).valueChanges({ idField: 'id' });

This is an example of multiple tags...

You could easily change this for any non-angular framework.

For OR queries (which can't be done with multiple where clauses), see here.

Solution 6 - Javascript

For example, there's an array look like this

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',
  },
]

Then you can just put the collection which one you want to set query's conditions into this funcion.

function* multipleWhere(
  collection,
  conditions = [{ field: '[doc].[field name]', operator: '==', value: '[any value]' }],
) {
  const pop = conditions.pop()
  if (pop) {
    yield* multipleWhere(
      collection.where(pop.key, pop.operator, pop.value),
      conditions,
    )
  }
  yield collection
}

You will get the collection set query's conditions.

Solution 7 - Javascript

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

Solution 8 - Javascript

Note that a multiple WHERE clause is inherently an AND operation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrendomView Question on Stackoverflow
Solution 1 - JavascriptDoug StevensonView Answer on Stackoverflow
Solution 2 - Javascriptdanday74View Answer on Stackoverflow
Solution 3 - JavascriptKai - Kazuya ItoView Answer on Stackoverflow
Solution 4 - JavascriptAbkView Answer on Stackoverflow
Solution 5 - JavascriptJonathanView Answer on Stackoverflow
Solution 6 - Javascript吳約南View Answer on Stackoverflow
Solution 7 - JavascriptAmar AmrouzView Answer on Stackoverflow
Solution 8 - JavascriptAdrian BartholomewView Answer on Stackoverflow