Bird
Raised Fist0
Astroframework~20 mins

Querying collection entries in Astro - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Collection Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Querying entries with a specific tag

Given a collection of blog posts, which query will return only the posts tagged with "travel"?

Astro
const travelPosts = Astro.fetchContent('posts').where(post => post.tags.includes('travel'))
AAstro.fetchContent('posts').filter(post => post.tags === 'travel')
BAstro.fetchContent('posts').where(post => post.tags.includes('travel'))
CAstro.fetchContent('posts').where(post => post.tags == 'travel')
DAstro.fetchContent('posts').filter(post => post.tags.contains('travel'))
Attempts:
2 left
💡 Hint

Remember, tags is an array. Use a method that checks if an array contains a value.

❓ query_result
intermediate
2:00remaining
Sorting collection entries by date

Which query will return the collection entries sorted from newest to oldest by their date field?

Astro
const sortedEntries = Astro.fetchContent('articles').sort((a, b) => new Date(b.date) - new Date(a.date))
AAstro.fetchContent('articles').orderBy('date', 'asc')
BAstro.fetchContent('articles').orderBy('date', 'desc')
CAstro.fetchContent('articles').sort((a, b) => new Date(a.date) - new Date(b.date))
DAstro.fetchContent('articles').sort((a, b) => new Date(b.date) - new Date(a.date))
Attempts:
2 left
💡 Hint

To sort from newest to oldest, subtract the first date from the second.

📝 Syntax
advanced
2:00remaining
Identifying syntax error in collection query

Which option contains a syntax error when querying collection entries?

Astro
const entries = Astro.fetchContent('notes').where(entry => entry.title.startsWith('A'))
AAstro.fetchContent('notes').where(entry => entry.title.startWith('A'))
B))'A'(htiWstrats.eltit.yrtne >= yrtne(erehw.)'seton'(tnetnoChctef.ortsA
CAstro.fetchContent('notes').filter(entry => entry.title.startsWith('A'))
DAstro.fetchContent('notes').where(entry => entry.title.startsWith('A'))
Attempts:
2 left
💡 Hint

Check the method name carefully for typos.

❓ optimization
advanced
2:00remaining
Optimizing query for entries with multiple conditions

You want to fetch entries from the recipes collection that are tagged with "vegan" and have a rating greater than 4. Which query is the most efficient and correct?

AAstro.fetchContent('recipes').where(entry => entry.tags.includes('vegan') && entry.rating > 4)
BAstro.fetchContent('recipes').filter(entry => entry.tags.includes('vegan')).filter(entry => entry.rating > 4)
CAstro.fetchContent('recipes').where(entry => entry.tags.includes('vegan')).where(entry => entry.rating > 4)
DAstro.fetchContent('recipes').filter(entry => entry.tags.includes('vegan') && entry.rating > 4)
Attempts:
2 left
💡 Hint

Try to combine conditions in a single query method when possible.

🧠 Conceptual
expert
2:00remaining
Understanding lazy evaluation in collection queries

Astro's fetchContent returns a lazy collection. What does this mean when chaining multiple query methods?

AEach query method runs separately and fetches data multiple times.
BThe queries run immediately and return all data at once.
CThe queries are combined and run only when the data is needed, improving performance.
DThe collection is copied each time a query method is called.
Attempts:
2 left
💡 Hint

Think about when data is actually fetched in lazy evaluation.

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

  1. Step 1: Understand the purpose of where

    The where method is used to select only those entries that meet a specific condition.
  2. Step 2: Compare with other options

    Deleting, adding, or sorting entries are different operations not done by where.
  3. Final Answer:

    It filters entries based on a given condition. -> Option A
  4. 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

  1. Step 1: Identify correct function usage

    The where method expects a function that takes an entry and returns a boolean condition.
  2. 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.
  3. Final Answer:

    collection.where(entry => entry.status == "active") -> Option A
  4. Quick Check:

    Correct arrow function syntax = collection.where(entry => entry.status == "active") [OK]
Hint: Use arrow function with entry parameter in where [OK]
Common Mistakes:
  • Using assignment (=) instead of comparison (==)
  • Omitting the entry parameter in the function
  • Passing condition directly without a function
3. Given the collection entries:
[{ id: 1, type: "fruit" }, { id: 2, type: "vegetable" }, { id: 3, type: "fruit" }]

What will collection.where(entry => entry.type == "fruit") return?
medium
A. []
B. [{ id: 1, type: "fruit" }, { id: 3, type: "fruit" }]
C. [{ id: 2, type: "vegetable" }]
D. SyntaxError

Solution

  1. Step 1: Identify entries matching the condition

    Entries with type == "fruit" are those with id 1 and 3.
  2. Step 2: Understand where output

    The where method returns a list of matching entries, so it returns the two fruit entries.
  3. Final Answer:

    [{ id: 1, type: "fruit" }, { id: 3, type: "fruit" }] -> Option B
  4. Quick Check:

    Filter fruits = entries 1 and 3 [OK]
Hint: Filter entries by condition to get matching list [OK]
Common Mistakes:
  • Returning non-matching entries
  • Expecting a single entry instead of a list
  • Confusing syntax error with correct code
4. What is wrong with this query?
collection.where(entry => entry.status = "active")
medium
A. The where method does not accept functions.
B. The arrow function is missing parentheses.
C. It uses assignment (=) instead of comparison (==).
D. The string "active" should be in single quotes.

Solution

  1. Step 1: Identify the operator used

    The query uses a single equals sign (=), which assigns a value instead of comparing.
  2. Step 2: Understand correct comparison syntax

    To compare, double equals (==) must be used inside the function.
  3. Final Answer:

    It uses assignment (=) instead of comparison (==). -> Option C
  4. Quick Check:

    Use == for comparison, not = [OK]
Hint: Use == for comparison inside where functions [OK]
Common Mistakes:
  • Using = instead of == causes wrong filtering
  • Thinking parentheses are required around single parameter
  • Believing where can't take functions
5. You want to find all entries where score is greater than 50 and passed is true. Which query correctly does this?
hard
A. collection.where(entry => entry.score > 50, entry.passed == true)
B. collection.where(entry => entry.score > 50 || entry.passed == true)
C. collection.where(entry => entry.score > 50 & entry.passed == true)
D. collection.where(entry => entry.score > 50 && entry.passed == true)

Solution

  1. Step 1: Understand logical AND condition

    To require both conditions, use the logical AND operator &&.
  2. Step 2: Check each option's logic

    collection.where(entry => entry.score > 50 && entry.passed == true) uses && correctly. collection.where(entry => entry.score > 50 || entry.passed == true) uses OR (||), collection.where(entry => entry.score > 50 & entry.passed == true) uses bitwise AND (&) which is incorrect here, and collection.where(entry => entry.score > 50, entry.passed == true) has invalid syntax with two parameters.
  3. Final Answer:

    collection.where(entry => entry.score > 50 && entry.passed == true) -> Option D
  4. Quick Check:

    Use && for AND conditions in where [OK]
Hint: Use && to combine multiple conditions inside where [OK]
Common Mistakes:
  • Using || instead of && for AND condition
  • Using bitwise & instead of logical &&
  • Passing multiple arguments instead of one function
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard