Set Which Fields Are Returned
If the query bar displays the Project option, you can specify which fields to return in the resulting data. By default, all fields are returned.
To set a projection:
In the Query Bar, click Options.
Enter the projection document into the Project field.
- To include fields:
Specify the field name and set to
1
in the project document.Example
{ year: 1, name: 1 } Only the fields specified in the project document are returned. The
_id
field is returned unless it is set to0
in the Project document.- To exclude fields:
Specify the field name and set to
0
in the project document.Example
{ year: 0, name: 0 } All fields except for the fields specified in the project document are returned.
As you type, the Find button is disabled and the Project label turns red until a valid query is entered.
Click Find to run the query and view the updated results.
Note
For query result sets larger than 1000 documents, Compass shows a subset of the results. Otherwise, Compass shows the entire result set.
For details on sampling, see Sampling.
How Does the Compass Query Compare to MongoDB and SQL Queries?
$project
corresponds to choosing specific fields to return
in a SQL SELECT
statement.
Example
You have 3,235 articles. You would like to see only the headlines and authors of those articles.
- SQL
SELECT headline, author FROM article; - MongoDB Aggregation
db.article.aggregate( { $project : { headline : 1, author : 1 } } ); - Compass Project Option
{ headline : 1, author : 1 }
Learn More
To learn how project works, see the project
entry in the
MongoDB Manual.