Bird
Raised Fist0
Astroframework~5 mins

Querying collection entries in Astro

Choose your learning style10 modes available
Introduction
Querying collection entries helps you find specific items in a group of data quickly and easily.
You want to show blog posts written by a certain author.
You need to list products in a category on an online store.
You want to display events happening on a specific date.
You want to filter recipes by ingredient.
You want to find all articles tagged with a certain keyword.
Syntax
Astro
const entries = Astro.fetchContent('collectionName').where(entry => entry.field === 'value');
Replace 'collectionName' with the name of your collection.
The 'where' method filters entries based on the condition inside the function.
Examples
Finds all posts where the author is Alice.
Astro
const postsByAlice = Astro.fetchContent('posts').where(post => post.author === 'Alice');
Gets all products marked as featured.
Astro
const featuredProducts = Astro.fetchContent('products').where(product => product.isFeatured === true);
If no events match the date, the result will be an empty list.
Astro
const emptyResult = Astro.fetchContent('events').where(event => event.date === '2025-01-01');
Finds the recipe with the specific id 'chocolate-cake'.
Astro
const singleEntry = Astro.fetchContent('recipes').where(recipe => recipe.id === 'chocolate-cake');
Sample Program
This program fetches all posts, then filters to show only posts written by Bob.
Astro
const allPosts = Astro.fetchContent('posts');
console.log('All posts:', allPosts);

const postsByBob = Astro.fetchContent('posts').where(post => post.author === 'Bob');
console.log('Posts by Bob:', postsByBob);
OutputSuccess
Important Notes
Querying collections is fast because it only looks at the data you need.
If no entries match your condition, you get an empty list, not an error.
Use clear and simple conditions to avoid confusion.
Summary
Querying collection entries helps you find specific data easily.
Use the 'where' method with a condition to filter entries.
If no entries match, you get an empty list, which is safe to handle.

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