For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.

Project Fields to Return from Query


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples.


➤ Use the Select your language drop-down menu in the upper-right to set the language of the following examples or select MongoDB Compass.


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.

This page provides examples of query operations with projection using the db.collection.find() method in mongosh.

db.inventory.insertMany( [ { item: "journal", status: "A", size: { h: 14, w: 21, uom: "cm" }, instock: [ { warehouse: "A", qty: 5 } ] }, { item: "notebook", status: "A", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "C", qty: 5 } ] }, { item: "paper", status: "D", size: { h: 8.5, w: 11, uom: "in" }, instock: [ { warehouse: "A", qty: 60 } ] }, { item: "planner", status: "D", size: { h: 22.85, w: 30, uom: "cm" }, instock: [ { warehouse: "A", qty: 40 } ] }, { item: "postcard", status: "A", size: { h: 10, w: 15.25, uom: "cm" }, instock: [ { warehouse: "B", qty: 15 }, { warehouse: "C", qty: 35 } ] }]);

If you do not specify a projection document, the db.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":

db.inventory.find( { status: "A" } )

The operation corresponds to the following SQL statement:

SELECT * from inventory WHERE status = "A"

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.

db.inventory.find( { status: "A" }, { item: 1, status: 1 } )

The operation corresponds to the following SQL statement:

SELECT _id, item, status from inventory WHERE status = "A"

You can remove the _id field from the results by setting it to 0 in the projection, as in the following example:

db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )

The operation corresponds to the following SQL statement:

SELECT item, status from inventory WHERE status = "A"

Note

With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection documents.

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:

db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )

Note

With the exception of the _id field, you cannot combine inclusion and exclusion statements in projection 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 the size document.

The uom field remains embedded in the size document.

db.inventory.find( { status: "A" }, { item: 1, status: 1, "size.uom": 1 })

You can also specify embedded fields using the nested form. For example, { item: 1, status: 1, size: { uom: 1 } }.

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:

db.inventory.find( { status: "A" }, { "size.uom": 0 })

You can also specify embedded fields using the nested form. For example, { size: { uom: 0 } }.

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 the instock array.

db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } )

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:

db.inventory.find( { status: "A" }, { item: 1, status: 1, instock: { $slice: -1 } } )

$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.

You can specify aggregation expressions in a query projection. Aggregation expressions let you project new fields and modify the values of existing fields.

For example, the following operation uses aggregation expressions to override the value of the status field, and project new fields area and reportNumber.

Note

The following example uses MongoDB Shell syntax. For driver examples of projection with aggregation, see your driver documentation.

db.inventory.find( { }, { _id: 0, item: 1, status: { $switch: { branches: [ { case: { $eq: [ "$status", "A" ] }, then: "Available" }, { case: { $eq: [ "$status", "D" ] }, then: "Discontinued" }, ], default: "No status found" } }, area: { $concat: [ { $toString: { $multiply: [ "$size.h", "$size.w" ] } }, " ", "$size.uom" ] }, reportNumber: { $literal: 1 } })
[ { item: 'journal', status: 'Available', area: '294 cm', reportNumber: 1 }, { item: 'planner', status: 'Discontinued', area: '685.5 cm', reportNumber: 1 }, { item: 'notebook', status: 'Available', area: '93.5 in', reportNumber: 1 }, { item: 'paper', status: 'Discontinued', area: '93.5 in', reportNumber: 1 }, { item: 'postcard', status: 'Available', area: '152.5 cm', reportNumber: 1 }]
  • When you use a $project aggregation stage it should typically be the last stage in your pipeline, used to specify which fields to return to the client.

    Using a $project stage 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.