MongoDB

Stage 2 · Lesson 4 of 9

Projection: choosing the fields you get back

Reading and writing documents

The second argument to find is a , which chooses the returned. { title: 1, year: 1 } returns only those fields plus _id, and { plot: 0 } returns everything except plot. You cannot mix including and excluding in one projection, except that you may exclude _id in an inclusion projection with _id: 0.

works for nested fields, and $slice returns part of an . Projection matters more than it looks. Documents can be large, and returning only what a page needs saves network, memory and time, and it makes covered queries possible in the indexing stage.

Common trapfetching whole documents everywhere because it is easier, then wondering why a list page moves megabytes of data it never shows.

A question to check yourself, an AI practice session and 1 cheat sheet entry come with the full roadmap. Unlock

Resources for this stage

Start with

  • CourseFree
    MongoDB University

    MongoDB

    Free: the CRUD lessons on inserting, finding, replacing, updating and deleting documents, with labs

Practise

  • Test filters and updates on your own sample documents and share the link when you get stuck

Keep handy

Mark this stage as not done?