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.
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 ( Without Projection )
db.inventory.find( { status: "A" } )
Return the Specified Fields and the _id
Field Only
db.inventory.find( { status: "A" }, { item: 1, status: 1 } )
Suppress _id
Field
db.inventory.find( { status: "A" }, { item: 1, status: 1, _id: 0 } )
Return All But the Excluded Fields
db.inventory.find( { status: "A" }, { status: 0, instock: 0 } )
Return Specific Fields in Embedded Documents
db.inventory.find( { status: "A" }, { item: 1, status: 1, "size.uom": 1 } )
Suppress Specific Fields in Embedded Documents
db.inventory.find( { status: "A" }, { "size.uom": 0 } )
References
https://www.mongodb.com/docs/manual/tutorial/project-fields-from-query-results/