Introduction
Querying collection entries helps you find specific items in a group of data quickly and easily.
const entries = Astro.fetchContent('collectionName').where(entry => entry.field === 'value');
const postsByAlice = Astro.fetchContent('posts').where(post => post.author === 'Alice');
const featuredProducts = Astro.fetchContent('products').where(product => product.isFeatured === true);const emptyResult = Astro.fetchContent('events').where(event => event.date === '2025-01-01');
const singleEntry = Astro.fetchContent('recipes').where(recipe => recipe.id === 'chocolate-cake');
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);
where method do when querying collection entries in Astro Framework?wherewhere method is used to select only those entries that meet a specific condition.where.where filters entries [OK]where means filter entries by condition [OK]where with adding or deleting entrieswhere sorts entrieswhere modifies the collection permanentlystatus equals "active"?where method expects a function that takes an entry and returns a boolean condition.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.collection.where(entry => entry.status == "active") [OK]where [OK][{ id: 1, type: "fruit" }, { id: 2, type: "vegetable" }, { id: 3, type: "fruit" }]collection.where(entry => entry.type == "fruit") return?type == "fruit" are those with id 1 and 3.where outputwhere method returns a list of matching entries, so it returns the two fruit entries.collection.where(entry => entry.status = "active")
where functions [OK]where can't take functionsscore is greater than 50 and passed is true. Which query correctly does this?&&.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.where [OK]where [OK]Pass the test and this pattern moves unknown