- MongoDB CRUD Operations >
- Query Documents >
- Project Fields to Return from Query
Project Fields to Return from Query¶
This page provides examples in:
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.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
This page provides examples of query operations with projection using the
db.collection.find()
method in the
mongo
shell. The examples on this page use the
inventory
collection. To populate the inventory
collection, run the following:
This page provides examples of query operations with projection using
MongoDB Compass. The examples on this
page use the inventory
collection. Populate the
inventory
collection with the following documents:
This page provides examples of query operations with projection using the
pymongo.collection.Collection.find()
method in the
PyMongo
Python driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations with projection using the com.mongodb.client.MongoCollection.find method in the MongoDB Java Synchronous Driver.
Tip
The driver provides com.mongodb.client.model.Filters helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations with projection using the
Collection.find() method in
the MongoDB Node.js Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
MongoDB\Collection::find()
method in the
MongoDB PHP Library.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
motor.motor_asyncio.AsyncIOMotorCollection.find()
method in the Motor
driver. The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations with projection using the com.mongodb.reactivestreams.client.MongoCollection.find method in the MongoDB Java Reactive Streams Driver.
The examples on this page use the inventory
collection. To populate the inventory
collection, run the
following:
This page provides examples of query operations with projection using the
MongoCollection.Find()
method in the
MongoDB C# Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
MongoDB::Collection::find() method
in the
MongoDB Perl Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
Mongo::Collection#find()
method in the
MongoDB Ruby Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
collection.find() method
in the
MongoDB Scala Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
This page provides examples of query operations with projection using the
Collection.Find
function in the
MongoDB Go Driver.
The examples on this page use the inventory
collection. To
populate the inventory
collection, run the following:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
You can run the operation in the web shell below:
For instructions on inserting documents in MongoDB Compass, see Insert Documents.
Return All Fields in Matching Documents¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
If you do not specify a projection document, the
db.collection.find()
method returns all fields in the
matching documents.
If you do not specify a projection document, Compass returns all fields in the matching documents.
If you do not specify a projection document, the
find()
method returns
all fields in the matching documents.
If you do not specify a projection, the com.mongodb.client.MongoCollection.find method returns all fields in the matching documents.
If you do not specify a projection document, the find() method yields all fields in the matching documents.
If you do not specify a projection document, the
find()
method returns all fields in the matching documents.
If you do not specify a projection, the com.mongodb.reactivestreams.client.MongoCollection.find method returns all fields in the matching documents.
If you do not specify a projection filter, the MongoCollection.Find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection document, the find() method returns all fields in the matching documents.
If you do not specify a projection, the collection.find() method returns all fields in the matching documents.
The following example returns all fields from all documents in the
inventory
collection where the status
equals "A"
:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Find.
The operation corresponds to the following SQL statement:
Return the Specified Fields and the _id
Field Only¶
A projection can explicitly include several fields by setting the
<field>
to 1
in the projection document. The following
operation returns all documents that match the query. In the result
set, only the item
, status
and, by default, the _id
fields
return in the matching documents.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
The operation corresponds to the following SQL statement:
Suppress _id
Field¶
You can remove the _id
field from the results by setting it to
0
in the projection, as in the following example:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
The operation corresponds to the following SQL statement:
Note
With the exception of the _id
field, you cannot combine inclusion
and exclusion statements in projection documents.
Return All But the Excluded Fields¶
Instead of listing the fields to return in the matching document, you
can use a projection to exclude specific fields. The following example
which returns all fields except for the status
and the instock
fields in the matching documents:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
Note
With the exception of the _id
field, you cannot combine inclusion
and exclusion statements in projection documents.
Return Specific Fields in Embedded Documents¶
You can return specific fields in an embedded document. Use the
dot notation to refer to the embedded
field and set to 1
in the projection document.
The following example returns:
- The
_id
field (returned by default), - The
item
field, - The
status
field, - The
uom
field in thesize
document.
The uom
field remains embedded in the size
document.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
Starting in MongoDB 4.4, you can also specify embedded fields using the
nested form, e.g. { item: 1, status: 1, size: { uom: 1 } }
.
Suppress Specific Fields in Embedded Documents¶
You can suppress specific fields in an embedded document. Use the
dot notation to refer to the embedded
field in the projection document and set to 0
.
The following example specifies a projection to exclude the uom
field inside the size
document. All other fields are returned in
the matching documents:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
Starting in MongoDB 4.4, you can also specify embedded fields using the
nested form, e.g. { size: { uom: 0 } }
.
Projection on Embedded Documents in an Array¶
Use dot notation to project specific fields inside documents embedded in an array.
The following example specifies a projection to return:
- The
_id
field (returned by default), - The
item
field, - The
status
field, - The
qty
field in the documents embedded in theinstock
array.
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
Project Specific Array Elements in the Returned Array¶
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
{ "instock.0": 1 }
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
{ "instock.0": 1 }
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
include("instock.0")
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
{ "instock.0": 1 }
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
[ "instock.0" => 1 ]
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
include("instock.0")
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array.
For example, the following operation will not project the array with the first element:
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
{ "instock.0" => 1 }
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
{ "instock.0" => 1 }
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
include("instock.0")
projection will not project the array
with the first element.
For fields that contain arrays, MongoDB provides the following
projection operators for manipulating arrays: $elemMatch
,
$slice
, and $
.
The following example uses the $slice
projection operator
to return the last element in the instock
array:
- Mongo Shell
- Compass
- Python
- Java (Sync)
- Node.js
- Other
Copy the following expression into the Filter field:
Click Options to open the additional query options.
Copy the following expression into the Project field:
Click Find.
To specify a projection document, chain the
com.mongodb.client.FindIterable.projection method to the
find
method. The example uses the
com.mongodb.client.model.Projections class to create the
projection documents.
$elemMatch
, $slice
, and
$
are the only way to project specific elements
to include in the returned array. For instance, you cannot
project specific array elements using the array index; e.g.
include("instock.0")
projection will not project the array
with the first element.
Additional Considerations¶
Starting in MongoDB 4.4, MongoDB enforces additional restrictions with
regards to projections. See Projection Restrictions
for
details.
See also