Filtering and querying collections is common when building blogs, portfolios, or any content-driven website with Astro.
💼 Career
Understanding how to query and manipulate collections is essential for frontend developers working with modern static site generators and frameworks.
Progress0 / 4 steps
1
Create the blog collection with posts
Create a collection called blog with three posts having slugs post-one, post-two, and post-three. Each post should have frontmatter fields title, date, and published. Use these exact values:
- post-one: title "First Post", date "2024-01-01", published true - post-two: title "Second Post", date "2024-02-01", published false - post-three: title "Third Post", date "2024-03-01", published true
Astro
Hint
Define an exported constant array named blog with objects for each post including the exact fields and values.
2
Add a filter condition for published posts
Create a variable called publishedPosts that filters the blog collection to include only posts where published is true.
Astro
Hint
Use the filter method on blog to keep only posts with published set to true.
3
Extract titles and dates of published posts
Create a variable called publishedTitlesAndDates that maps over publishedPosts to create a new array of objects containing only the title and date fields.
Astro
Hint
Use map on publishedPosts to create objects with only title and date.
4
Export the final filtered and mapped data
Export the variable publishedTitlesAndDates so it can be used in your Astro components.
Astro
Hint
Use an export statement to export publishedTitlesAndDates.
Practice
(1/5)
1. What does the where method do when querying collection entries in Astro Framework?
easy
A. It filters entries based on a given condition.
B. It deletes entries from the collection.
C. It adds new entries to the collection.
D. It sorts entries alphabetically.
Solution
Step 1: Understand the purpose of where
The where method is used to select only those entries that meet a specific condition.
Step 2: Compare with other options
Deleting, adding, or sorting entries are different operations not done by where.
Final Answer:
It filters entries based on a given condition. -> Option A
Quick Check:
where filters entries [OK]
Hint: Remember: where means filter entries by condition [OK]
Common Mistakes:
Confusing where with adding or deleting entries
Thinking where sorts entries
Assuming where modifies the collection permanently
2. Which of the following is the correct syntax to filter entries where the field status equals "active"?
easy
A. collection.where(entry => entry.status == "active")
B. collection.filter(status = "active")
C. collection.where(status == "active")
D. collection.where(entry.status = "active")
Solution
Step 1: Identify correct function usage
The where method expects a function that takes an entry and returns a boolean condition.
Step 2: Check each option's syntax
collection.where(entry => entry.status == "active") correctly uses a function with an arrow and compares entry.status to "active". Others have syntax errors or wrong usage.
Final Answer:
collection.where(entry => entry.status == "active") -> Option A