Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Dynamically adding properties to database query in JavaScript

I am writing a function to query a Firestore database collection for documents that have only certain properties. The filters is defined as an array of "key, value" pairs. eg:

[
  ["Colour", "green"],
  ["Colour", "blue"],
  ["Greeting", "hello"]
]

This array can be of any length, and I am trying to get every document in the database that does not have values listed in the filter array.

I can do this by using:

await db.collection("database")
  .where("Colour", "!=", "blue")
  .where("Colour", "!=", "green")
  .where("Greeting", "!=", "hello")
  .get()

My issue is that the filter can be any length, so I cannot write the query to have a set number of .where() methods. Is there any way in JavaScript that I can dynamically add methods to a query like shown above (not knowing how many methods I need to add)?

My workaround right now is just to query the entire database, then sort it using Javascript filter functions, but I would like to only have to query the database for the values needed.

Alternatively, are there any other Firestore queries that could complete this filter? I was looking at the docs, but the way my filter is set up with key/value pairs that could be repeated or undefined, it did not seem that any of the complex query methods would work.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming that you are building an array full of only excluded key-value pairs and the values you are excluding are properly indexed, we can start off defining some constants:

const collectionRef = db.collection("database");

const excludedKeyValuePairs = [
  ["Colour", "green"],
  ["Colour", "blue"],
  ["Greeting", "hello"],
]

Now we have those, we can build the query using Array#reduce.

const query = excludedKeyValuePairs
  .reduce(
    (query, [key, value]) => query.where(key, "!=", value), // appends the new constraint, returning the new query object
    collectionRef
  );

const querySnapshot = await query.get();

However, if you can use the newer modular Firestore SDK, you can also achieve the same result using:

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

const db = getFirestore();
const collectionRef = collection(db, "database");
const constraints = [
  where("Colour", "!=", "green"),
  where("Colour", "!=", "blue"),
  where("Greeting", "!=", "Hello")
  // elements can also be added or removed using standard array methods as needed.
]
// OR const constraints = excludedKeyValuePairs.map(([key, value]) => where(key, "!=", value))

const querySnapshot = await getDocs(query(collectionRef, ...constraints));
about 4 years ago · Juan Pablo Isaza Report

0

You can map from your array of conditions to where clauses with:

const exclusions = [
  ["Colour", "green"],
  ["Colour", "blue"],
  ["Greeting", "hello"],
]

let collectionRef = db.collection("database");
const conditions = exclusions.map((e) => where(e[0], "!=", e[1]));
let query = query(collectRef, conditions);

const querySnapshot = await getDocs(query);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!