📖 Scenario: You are building a simple app to display a list of books stored in Firebase Firestore. You want to show the books ordered by their publishedYear from oldest to newest.
🎯 Goal: Learn how to query a Firestore collection and order the results by a specific field.
📋 What You'll Learn
Create a Firestore collection reference to books
Add a configuration variable for the order field publishedYear
Write a query that orders the books collection by publishedYear ascending
Complete the query by getting the documents ordered correctly
💡 Why This Matters
🌍 Real World
Ordering query results is common when displaying lists like products, posts, or books sorted by date or rating.
💼 Career
Understanding how to order Firestore queries is essential for building responsive and user-friendly cloud applications.
Progress0 / 4 steps
1
Create a Firestore collection reference
Create a variable called booksRef that references the Firestore collection named books using firebase.firestore().collection('books').
Firebase
Hint
Use firebase.firestore().collection('books') to get the collection reference.
2
Add the order field configuration
Create a constant called orderField and set it to the string 'publishedYear' to specify the field to order by.
Firebase
Hint
Define orderField as 'publishedYear'.
3
Write the ordered query
Create a variable called orderedQuery that uses booksRef.orderBy(orderField, 'asc') to order the books by publishedYear in ascending order.
Firebase
Hint
Use orderBy with orderField and 'asc' to order ascending.
4
Complete the query by getting the documents
Use orderedQuery.get() to retrieve the ordered documents from Firestore and assign it to a variable called querySnapshot.
Firebase
Hint
Use await orderedQuery.get() to fetch the documents.
Practice
(1/5)
1. What does the orderBy method do in Firebase Firestore queries?
easy
A. It deletes documents from the collection.
B. It filters documents based on a condition.
C. It sorts the query results by a specified field.
D. It adds new documents to the collection.
Solution
Step 1: Understand the purpose of orderBy
The orderBy method is used to sort documents in a query result by a specific field.
Step 2: Differentiate from other query methods
Filtering uses where, deleting uses delete, and adding uses add. These are different from sorting.
Final Answer:
It sorts the query results by a specified field. -> Option C
Quick Check:
Ordering = Sorting [OK]
Hint: Remember: orderBy sorts, where filters [OK]
Common Mistakes:
Confusing orderBy with filtering
Thinking orderBy adds or deletes data
Assuming orderBy changes data, not just order
2. Which of the following is the correct syntax to order Firestore documents by the field age in descending order?
easy
A. db.collection('users').orderBy('age', 'desc')
B. db.collection('users').orderBy('age', descending)
C. db.collection('users').orderBy('age', 'descending')
D. db.collection('users').orderBy('age', -1)
Solution
Step 1: Recall Firestore orderBy syntax
The correct syntax uses the string 'desc' for descending order, not 'descending' or other variants.
Step 2: Check each option
db.collection('users').orderBy('age', 'desc') uses 'desc' correctly. Options A and B use invalid strings. db.collection('users').orderBy('age', -1) uses a number which is not valid in Firestore.
Final Answer:
db.collection('users').orderBy('age', 'desc') -> Option A
Quick Check:
Descending order = 'desc' string [OK]
Hint: Use 'asc' or 'desc' strings exactly in orderBy [OK]