You can use projection to select which document fields to return from a query by using the following methods:
Your programming language's driver.
The MongoDB Atlas UI. To learn more, see Project Fields to Return from a Query with MongoDB Atlas.
By default, queries in MongoDB return all fields in matching documents. To limit the amount of data that MongoDB sends to applications, you can include a projection document to specify or restrict fields to return.
Additional Considerations
When you use a
$projectaggregation stage it should typically be the last stage in your pipeline, used to specify which fields to return to the client.Using a
$projectstage at the beginning or middle of a pipeline to reduce the number of fields passed to subsequent pipeline stages is unlikely to improve performance, because the database performs this optimization automatically.MongoDB enforces additional restrictions with regards to projections. See Projection Restrictions for details.