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

245
Vistas
Firebases Firestore: How do you get all the documents in a collection in order?

I'd like to create a room based game where you can create and join rooms on your own. Firebases Firestore is my go to because it is cheap and easy to implement in React Native.

The creating user should't have to input their desired room ID, but the room ID should be created dynamically. Since I don't want the joining user to type in 16 digits or so, I can't use the autogeneration of document ids.

Since randomizing these room ids and checking for their existance can take a really long time if unlucky, I approached it like the following: I'd like to perform a simple query on every document in a collection. Order the documents, limit them and get the biggest number. Afterwards this number will be incremented and be set as the new rooms document id. (If there is a more conventient way of doing so feel free to suggest it)

So far I got every document like so:

const querySnapshot = await getDocs(collection(db, "rooms"));

And tried to perform a query on the result:

const q = query(querySnapshot, orderBy("", "asc"), limit(3));

Which resulted in an error Function orderBy() called with invalid data. Invalid field path (). Paths must not be empty, begin with '.', end with '.', or contain '..'.

I also tried the following:

const roomsRef = db.collection('rooms')
const q = query(roomsRef, orderBy("", "asc"), limit(3));

Which throwed another error: TypeError: _firebase.db.collection is not a function.

Firebase.js:

import { initializeApp } from "firebase/app";
import { getFirestore } from "firebase/firestore";
// Add SDKs for Firebase products that you want to use
// https://firebase.google.com/docs/web/setup#available-libraries

// Your web app's Firebase configuration
const firebaseConfig = {
  apiKey: "xyz",
  authDomain: "xyz",
  projectId: "xyz",
  storageBucket: "xyz",
  messagingSenderId: "xyz",
  appId: "xyz",
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
export const db = getFirestore(app); 

Firestore: Firebase Firestore

Help is very much appreciated.

Edit: I came up with a similar solution which didn't include any limit or orderby: Since Firebase is ordering the documents by default, the only thing I needed to do is getting the last element of my documents.

My Solution:

const querySnapshot = await getDocs(collection(db, "rooms"));
console.log(querySnapshot.docs[querySnapshot.size - 1].data());
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