Given a content collection named posts in Astro, which query will return all items sorted by their date field in descending order?
const posts = await getCollection('posts');
Remember that dates need to be converted to Date objects to compare properly.
Option C correctly sorts the posts by date descending by converting the date strings to Date objects and subtracting them. Other options either sort ascending or use string comparison which is unreliable for dates.
Which statement best describes the difference between content and data collections in Astro?
Think about the file types and usage for each collection type.
Content collections are designed for markdown files with frontmatter metadata, while data collections hold structured data like JSON or JavaScript objects without markdown formatting.
Which option correctly defines a schema for a data collection in Astro using zod to require a title string and an optional tags array of strings?
import { z } from 'astro:content'; export const schema =
Check the order of method calls and types inside the z.object.
Option D correctly requires title as a string and makes tags an optional array of strings. Other options have incorrect optional placement, wrong types, or invalid method chaining.
You want to fetch only the title and date fields from a large posts content collection to improve performance. Which approach is best?
Check if getCollection supports field selection to reduce data fetched.
Option A uses the built-in field selection to fetch only necessary fields, reducing memory and processing. Option A fetches all data then filters, which is less efficient. Option A delays filtering and wastes resources. Option A is inefficient for many posts.
You run this code to get a content collection but date is always undefined in results:
const posts = await getCollection('posts');
console.log(posts.map(p => p.data.date));What is the most likely cause?
Check if the schema controls which fields are available in data.
If the date field is not declared in the collection schema, Astro excludes it from the data object, causing it to be undefined even if present in frontmatter. Other options either cause errors or are unrelated.
