A collection is a group of content entries, like blog posts or products, organized together to make querying and managing easier.
Click to reveal answer
beginner
How do you query all entries from a collection in Astro?
Use getCollection('collectionName') to get all entries from that collection.
Click to reveal answer
intermediate
What does the filter option do when querying collection entries?
It lets you select only entries that match certain conditions, like filtering blog posts by a tag or date.
Click to reveal answer
intermediate
How can you sort collection entries when querying in Astro?
You can sort entries by a field, for example by date, using JavaScript array methods like .sort() after fetching entries.
Click to reveal answer
beginner
Why is querying collection entries useful in a website?
It helps you show only the content you want, like recent posts or products on sale, making your site dynamic and user-friendly.
Click to reveal answer
Which function fetches all entries from a collection in Astro?
AqueryCollection('collectionName')
BfetchEntries('collectionName')
CgetCollection('collectionName')
DloadEntries('collectionName')
✗ Incorrect
The correct function to get all entries is getCollection('collectionName').
How do you filter entries by a condition in Astro collection queries?
AUsing <code>map()</code> method
BUsing <code>sort()</code> method
CUsing <code>reduce()</code> method
DUsing the <code>filter</code> option with a function
✗ Incorrect
Filtering is done by passing a filter function that returns true for entries to keep.
What type of data structure do collection entries return as?
AArray of objects
BSingle object
CString
DNumber
✗ Incorrect
Collection entries are returned as an array of objects, each representing one entry.
Which JavaScript method is commonly used to sort collection entries after fetching?
Afilter()
Bsort()
Cpush()
Dpop()
✗ Incorrect
The sort() method sorts arrays, useful for ordering collection entries.
Why might you want to query collection entries with conditions?
ATo show only relevant content to users
BTo delete entries
CTo create new entries
DTo change the database schema
✗ Incorrect
Querying with conditions helps display only the content that fits the user's needs or context.
Explain how to fetch and filter entries from a collection in Astro Framework.
Think about how you pick only certain items from a list.
You got /3 concepts.
Describe why sorting collection entries is important and how you can do it in Astro.
Imagine arranging books on a shelf by date or name.
You got /3 concepts.
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