Given a collection of blog posts, which query will return only the posts tagged with "travel"?
const travelPosts = Astro.fetchContent('posts').where(post => post.tags.includes('travel'))
Remember, tags is an array. Use a method that checks if an array contains a value.
Option B correctly uses includes to check if the tags array contains 'travel'. Options A and C compare the array directly to a string, which is incorrect. Option B uses contains, which is not a valid method in JavaScript.
Which query will return the collection entries sorted from newest to oldest by their date field?
const sortedEntries = Astro.fetchContent('articles').sort((a, b) => new Date(b.date) - new Date(a.date))To sort from newest to oldest, subtract the first date from the second.
Option D correctly sorts the entries by date descending using sort with new Date(b.date) - new Date(a.date). Option D sorts ascending. Options B and D use orderBy, which is not a valid Astro method.
Which option contains a syntax error when querying collection entries?
const entries = Astro.fetchContent('notes').where(entry => entry.title.startsWith('A'))
Check the method name carefully for typos.
Option A uses startWith which is not a valid string method. The correct method is startsWith. Options A, B, and C use valid syntax.
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?
Try to combine conditions in a single query method when possible.
Option A uses a single where with both conditions combined, which is efficient and correct. Option A uses two filter calls, which works but is less efficient. Option A chains two where calls, which is invalid in Astro. Option A uses filter which is not a valid Astro method.
Astro's fetchContent returns a lazy collection. What does this mean when chaining multiple query methods?
Think about when data is actually fetched in lazy evaluation.
Lazy evaluation means the queries are not executed immediately but combined and run only when the data is accessed, which improves performance. Option C correctly describes this. Options B, C, and D misunderstand lazy evaluation.
