Question 7 / 50
Must KnowEasyConceptCRUD
Why bother with projections if you are going to drop fields in application code anyway?
Think about it first.
Short answer
Projection cuts what the server sends over the wire and what your process has to deserialise. It can also make a query covered, meaning MongoDB answers it from the index without fetching the document.
Why?
Inclusion and exclusion cannot be mixed, except excluding _id on an include list. _id is returned unless you set _id: 0. Returning a 2MB user profile to render a name in a list is a common production bug.
Example
db.users.find(
{ city: "Bangalore" },
{ name: 1, email: 1 }
)Interview tip
Tie projection to payload size and covered queries, not only to 'cleaner JSON'.
Common mistake
Projecting in the app after find({}). The damage is already done on the wire.
How did you do?