Mongoose and MongoDB patterns — schema design, validation, indexes, virtuals,
99
99%
Does it follow best practices?
Impact
100%
1.11xAverage score across 5 eval scenarios
Passed
No known issues
A social blogging platform is experiencing performance issues with their "recent posts" feed as their user base has grown to millions of posts. Their current implementation uses page numbers in the API (?page=5&limit=20) and users are reporting that later pages load extremely slowly or time out. The backend team also wants to refactor their data models before the issues get worse — currently, a Post document stores only a authorId reference field, but it always fetches the author's full profile separately via populate, even though the author's display name and avatar are small, stable pieces of data that appear next to every post.
Additionally, the platform has a Tag model with its own collection — tags can be attached to multiple posts and are browsed independently in the tag directory. There's debate on the team about whether to embed tags inside posts or keep them as references.
Your task is to design the data models and implement the feed endpoint logic.
Write a TypeScript file at feed-service.ts that includes:
Post Mongoose schema and model. The Post should include: a title, body, publication status (draft or published), the author's display information, and associated tags.Tag Mongoose schema and model (tags are browsed independently and reused across many posts).getFeed(afterId?) function that returns a page of published posts for the main feed. The feed must handle datasets with millions of posts efficiently. The function should accept an optional pagination token and return both the results and a token that can be used to fetch the next page.Make the data model design decisions explicitly — embed or reference — based on the nature of each relationship.