Project Fields to Return from Query
https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/
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.
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
.
The examples on this page use the inventory
collection. Connect to a test database in your MongoDB instance then create the inventory
collection:
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 } ] } |
]); |
Return All Fields in Matching Documents
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" |
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.
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" |
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:
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.
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:
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.
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.
db.inventory.find( |
{ status: "A" }, |
{ item: 1, status: 1, "size.uom": 1 } |
) |
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:
db.inventory.find( |
{ status: "A" }, |
{ "size.uom": 0 } |
) |
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.
db.inventory.find( { status: "A" }, { item: 1, status: 1, "instock.qty": 1 } ) |
Project Specific Array Elements in the Returned Array
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.
Additional Considerations
Starting in MongoDB 4.4, MongoDB enforces additional restrictions with regards to projections. See Projection Restrictions for details.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2020-08-13 CDT Downloads (Eclipse c++ plugin)
2020-08-13 How to make eclipse/pydev happy to see flask extensions on windows?
2014-08-13 [留学新生须知]新生美国生活常用单词汇总
2010-08-13 禁止双击选择页面内容
2010-08-13 7个JavaScript在IE和Firefox浏览器下的差异写法
2009-08-13 Google Job : Research Engineer - Beijing
2009-08-13 Google Job: Software Engineer - Beijing