Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

210
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda