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

212
Views
How to use query with random in firestore v9?

Let's say I have 9 docs in a collection, 3 of them contains section: 1, 3 of them contains section: 2, 3 of them contains section: 3.

I want to get two random docs that contain section: 2.

something like:

const colRef = collection(db, "readingfiles");

const q = query(colRef, where("section", "==", 2), orderBy("random"), limit(2));

getDocs(q).then((snapshot) => {
    snapshot.docs.forEach((doc) => {
        console.log(doc.data());
    })
}).catch((err) => {
    console.log(err.message);
});

One way I found is to give an Id to every document then use where( ) with random numbers to get data.

I wonder if there is any easy and effective way.
something like:

const q = query(colRef, where("section", "==", 2), getRandom(2));

or

const q = query(colRef, where("section", "==", 2), orderBy("__random__"), limit(2));
about 4 years ago · Juan Pablo Isaza
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!