Projection
Return only the fields your application needs.
Return only the fields your application needs. Fetching fewer fields saves network and memory, above all with large documents.
Requirements
- A connection with a user that has
readon the database.
Choose fields
JavaScript
db.orders.find({ status: "paid" }, { customer: 1, total: 1, _id: 0 });Trim an array
JavaScript
db.orders.find({}, { lines: { $slice: 3 } });Check
The returned documents carry only the fields you asked for. _id always comes out unless you exclude it with _id: 0.
Common problems
- The projection fails because it mixes included and excluded fields; only
_idcan be excluded next to included fields.