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

105
Views
Firestore query with empty string , 'any' or 'all'

I have filter on my website, I need to retrieve data based on the inputs in the filter. Some fields of the filter can be empty (''), I want to be able to ignore those queries related to those fields basically. Something like this

//fields
const { country, age, gender, contract, budget_low, budget_high } = queries;

//query
db.collection("users")
        .where("country", "in", country === "" ? "any" : "Nigeria")
        .where("gender", "in", gender === "" ? "any" : "Male")
        .where("age", "in", age === "" ? "any" : "21")
        .get()
        .then((querySnapshot) => {
          var data = [];
          querySnapshot.forEach((doc) => {
            data.push(doc.data());
          });
          setPeers(data);
        })
        .catch((error) => {
          console.log("Error getting documents: ", error);
        });

Is there a way to use 'any' or 'all' if the fields is empty (''), so that way I retrieve all data conditionally (if filter field exists)?

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

0

You'll want to dynamically create your query, and only add the conditions for the values that the user actually selected.

let query = db.collection("users");

if (country !== "") query = query.where("country", "==", country);
if (gender !== "") query = query.where("gender", "==", gender);
if (age !== "") query = query.where("age", "==", age);

query.get()...

In the new modular/v9 syntax, that'd be:

let conditions = []

if (country !== "") conditions.push(where("country", "==", country));
if (gender !== "") conditions.push(where("gender", "==", gender));
if (age !== "") conditions.push(where("age", "==", age));

let query = query(collection(db, "users"), ...conditions);

getDocuments(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!