Access sibling documents in the same collection
mainTo access other documents within the same collection (e.g., to find the 'previous' or 'next' post), use the collection.documents() function found on the collection object within the context.
Important:
collection.documents()is asynchronous.- It returns an array of documents in their untransformed state (matching the schema shape).
- Do not use the top-level
documents()function to access the same collection; usecollection.documents()instead.
const posts = defineCollection({
// ...
transform: async (doc, { collection }) => {
const docs = await collection.documents();
const idx = docs.findIndex((d) => doc._meta.filePath === d._meta.filePath);
return {
...doc,
prev: idx > 0 ? docs[idx - 1] : null,
next: idx < docs.length - 1 ? docs[idx + 1] : null,
};
},
});