In this tutorial, you’ll how to use the MongoDB projection that allows you to select fields to return from a query. In MongoDB, projection simply means selecting fields to return from a query.
To determine if a field is included in the returned documents, you use the following syntax: { <field>: value, ...}
If the value is 1 or true, the is included in the matching documents. In case the value is 0 or false, it is suppressed from the returned documents.
If the projection document is empty {}, all the available fields will be included in the returned documents.
To specify a field in an embedded document, you use the following dot notation:
{ "<embeddedDocument>.<field>": value, ... }
Similarly, to include a from an embedded document located in an array, you use the following dot notation syntax:
{ "<arrayField>.field": value, ...}
Launch mongo shell from the Terminal on Linux and connect to the productdb database on the local MongoDB server: mongosh productdb
We’ll use the following products collection for the projection examples:
db.products.insertMany([
{ "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate": ISODate("2011-05-14"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 },"color":["white","black"],"storage":[64,128,256],"inventory":[{ qty: 1200,"warehouse": "San Jose"}]},
{ "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate": ISODate("2011-09-01") , "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 },"color":["white","black","purple"],"storage":[128,256,512],"inventory":[{ qty: 300,"warehouse": "San Francisco"}]},
{ "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate": ISODate("2015-01-14"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 },"color":["blue"],"storage":[16,64,128],"inventory":[{ qty: 400,"warehouse": "San Jose"},{ qty: 200,"warehouse": "San Francisco"}]},
{ "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate": ISODate("2020-05-14"),"spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256,1024],"inventory":[{ qty: 1200,"warehouse": "San Mateo"}]},
{ "_id" : 5, "name" : "SmartPhone", "price" : 599,"releaseDate": ISODate("2022-09-14"), "spec" : { "ram" : 4, "screen" : 5.7, "cpu" : 1.66 },"color":["white","orange","gold","gray"],"storage":[128,256]}
])
The following query returns all fields from all documents in the products collection where the price is 899:
db.products.find({price: 899});
The following example returns all documents from the products collection. However, its result includes only the name, price, and _id field in the matching documents:
db.products.find({}, {
name: 1,
price: 1
});
To remove the _id field, you need to explicitly specify the _id field as 0:
db.products.find({}, {
name: 1,
price: 1,
_id: 0
});
The following example returns all the fields of the document _id 1 except the releaseDate, spec and storage fields in projecttion argument, then I will set the releaseDate, spec fields and storage is zero:
db.products.find({_id:1}, {
releaseDate: 0,
spec: 0,
storage: 0
})
The following example returns the name, price, and _id fields of document _id 1. It also returns the screen field on the spec embedded document:
db.products.find({_id:1}, {
name: 1,
price: 1,
"spec.screen": 1
})
MongoDB 4.4 and later allows you to specify embedded fields using the nested form like this:
db.products.find({_id:1}, {
name: 1,
price: 1,
spec : { screen: 1 }
})
The following example specifies a projection that returns:
- The _id field (by default)
- The name field
- And qty field in the documents embedded in the inventory array.
db.products.find({}, {
name: 1,
"inventory.qty": 1
});
Top comments (0)