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

252
Views
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
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!