{"success":true,"name":"project-only-the-mongodb-fields-the-screen-actually-needs","description":"Fetching MongoDB documents and using only a few of their fields","category":"ai-brimind","path":"ai-brimind/project-only-the-mongodb-fields-the-screen-actually-needs/SKILL.md","file":"SKILL.md","content":"---\nname: project-only-the-mongodb-fields-the-screen-actually-needs\ndescription: \"Fetching MongoDB documents and using only a few of their fields\"\n---\n\n# Project Only the MongoDB Fields the Screen Actually Needs\n\n## When to use\nProject uses MongoDB, and a query returns full documents while the code or screen uses a small subset.\n\n## Example request\nLoading full user docs for a name-only dropdown → projection { name: 1 } sends kilobytes instead of megabytes. Fetching docs with big embedded arrays for a list view → exclude the array in the projection; the list never shows it. A query that could be answered by the index alone → matching projection to index fields gives a covered query: no document fetch at all. Passing whole documents through API responses → the projection IS your response contract; unneeded fields leak data and bandwidth. Each maps to 'the projection is part of the query design, not an afterthought'.\n\n## Instructions\nAct as a developer who treats SELECT * as a bug, in MongoDB terms: no query ships without a projection decision. For each find or aggregation: (1) list the fields the consumer actually uses (screen, API response, computation), (2) add a projection ({ field: 1 }) or Mongoose .select() naming exactly those — explicitly excluding heavy embedded arrays and sensitive fields (password hashes, tokens) so they never leave the database, (3) check for the bonus case: if all projected fields plus the filter fields live in one index, the query is 'covered' and MongoDB answers from the index without touching documents at all. Full documents are allowed only when the code truly needs the whole shape — state it when so. If you do not know which fields the consumer needs, ask or read the consuming code first. Only finalize the query once the projection matches confirmed usage, and explain why the query moves only the data the feature really needs.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1051\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}