Querying Embedded Documents
Querying Embedded Documents
MongoDB provides you read operations to retrieve embedded/nested documents from the
collection or query a collection for a embedded/nested document. You can perform read
operations using the db.collection.find() method. This method selects or views
embedded/nested documents of the collection and returns the cursor to the selected document.
Syntax: db.collection.find(filter, projection)
Parameters:
filter: It is an optional parameter. It specifies the selection filter with the help of query
operators. And if you want to get all the documents present in the collection, then omit
these parameters or pass an empty document in the method. The type of this parameter is a
Document.
projection: It is an optional parameter. It specifies that only those fields return to the
document that matches the given query filter. And if you want to get all the fields in the
document, then omit this parameter. Learn more.
Return: This method returns a cursor to the documents that match the specified query criteria.
When you use find() method, it returns documents, which means the method is actually
returning the cursor to the documents.
Example 1:
db.inventory.insertMany( [
{ item: "journal", qty: 25, size: { h: 14, w: 21, uom: "cm" }, status: "A" },
{ item: "notebook", qty: 50, size: { h: 8.5, w: 11, uom: "in" }, status: "A" },
{ item: "paper", qty: 100, size: { h: 8.5, w: 11, uom: "in" }, status: "D" },
{ item: "planner", qty: 75, size: { h: 22.85, w: 30, uom: "cm" }, status: "D" },
{ item: "postcard", qty: 45, size: { h: 10, w: 15.25, uom: "cm" }, status: "A" }
]);
Match an Embedded/Nested Document
To specify an equality condition on a field that is an embedded/nested document, use the query
filter document { <field>: <value> } where <value> is the document to match.
For example, the following query selects all documents where the field size equals the
document { h: 14, w: 21, uom: "cm" }:
NOTE
When querying using dot notation, the field and nested field must be inside quotation marks.
The following example selects all documents where the field uom nested in the size field
equals "in":
A query filter document can use the query operators to specify conditions in the following form:
The following query uses the less than operator ($lt) on the field h embedded in the size field:
The following query selects all documents where the nested field h is less than 15, the nested
field uom equals "in", and the status field equals "D":
middle: "Kumar",
last: "Singh"}}).pretty()
Output:
Select documents that match the nested field (Using query operators) –
In this example, we are retrieving the documents that match the nested field using the query
operators. Here, in the query, we use $in operator. This operator is used to match any of the
values specified in the given array.
Output:
Select documents that match the nested fields (Using AND condition) –
In this example, we are retrieving the documents that match the nested fields.
"name.first": "Mohit"}).pretty()
Output:
"name.last": 1}).pretty()
Example 3:
Run the following command to insert some documents in the shapes collection.
> db.shapes.insertMany([
"shape": "rectangle",
"dim": {
"length": 10,
"breadth": 20,
"uom": "cm"
},
{
"shape": "rectangle",
"dim": {
"length": 20,
"breadth": 30,
"uom": "cm"
},
"shape": "rectangle",
"dim": {
"length": 5,
"breadth": 10,
"uom": "cm"
},
"shape": "square",
"dim": {
"side": 10.5,
"uom": "cm"
},
"shape": "square",
"dim": {
"side": 20,
"uom": "cm"
},
"shape": "square",
"dim": {
"side": 15,
"uom": "inch"
},
"shape": "square",
"item": "Sq 4",
"dim": {
"side": 25.5,
"uom": "inch"
]);
We learned how to create embedded documents in the Insert Document tutorial. Feel free to
check that out.
To find all the documents in the shapes collection that represents a rectangle we have to run the
following command.
"_id" : ObjectId("5d175dffba3250e57f98faca"),
"shape" : "rectangle",
"dim" : {
"length" : 10,
"breadth" : 20,
"uom" : "cm"
"_id" : ObjectId("5d175dffba3250e57f98facb"),
"shape" : "rectangle",
"dim" : {
"length" : 20,
"breadth" : 30,
"uom" : "cm"
"_id" : ObjectId("5d175dffba3250e57f98facc"),
"shape" : "rectangle",
"dim" : {
"length" : 5,
"breadth" : 10,
"uom" : "cm"
}
}
Next, we have to check the side field which is inside the dim field. So, we have to
use dim.side in our filter.
We have to make sure the side is at least 20 cm so, we have to use the $gte greater than or equal
to operator for the dim.side field.
And for the unit of measurement uom we have to check the dim.uom to be equal to cm.
> db.shapes.find({
"shape": "square",
"dim.side": { $gte: 20 },
"dim.uom": "cm"
}).pretty();
"_id" : ObjectId("5d175dffba3250e57f98face"),
"shape" : "square",
"dim" : {
"side" : 20,
"uom" : "cm"
shape is rectangle
dim.length is at least (i.e. greater than or equal to) 10
dim.breadth is at least (i.e. greater than or equal to) 20
dim.uom is cm
> db.shapes.find({
"shape": "rectangle",
"dim.length": { $gte: 10 },
"dim.breadth": { $gte: 20 },
"dim.uom": "cm"
}).pretty();
"_id" : ObjectId("5d175dffba3250e57f98faca"),
"shape" : "rectangle",
"dim" : {
"length" : 10,
"breadth" : 20,
"uom" : "cm"
"_id" : ObjectId("5d175dffba3250e57f98facb"),
"shape" : "rectangle",
"dim" : {
"length" : 20,
"breadth" : 30,
"uom" : "cm"
To update bested documents in MongDB, use UPDATE() and positional($) operator. Let us
create a collection with documents −
> db.demo643.insertOne({
... details : [
... {
... "CountryName":"US",
... StudentDetails:[{Name:"Chris"},{SubjectName:"MySQL"}]
... },
...
... {
... "CountryName":"UK",
... StudentDetails:[{Name:"Bob"},{SubjectName:"Java"}]
... }
... ]
... }
... )
"acknowledged" : true,
"insertedId" : ObjectId("5e9c737f6c954c74be91e6e3")
Display all documents from a collection with the help of find() method −
> db.demo643.find();
Display all documents from a collection with the help of find() method −
> db.demo643.find().pretty();
This will produce the following output −
{
"_id" : ObjectId("5e9c737f6c954c74be91e6e3"),
"details" : [
{
"CountryName" : "US",
"StudentDetails" : [
{
"Name" : "Chris"
},
{
"SubjectName" : "MySQL"
}
]
},
{
"CountryName" : "UK",
"StudentDetails" : [
{
"Name" : "Bob"
},
{
"SubjectName" : "Java"
},
{
"Marks" : 78
}
]
}
]
}