MongoDB
In this MongoDB tutorial we will learn to query embedded documents.
In the previous tutorial we learned about how to query documents and about logical operators and how to select specific fields. Feel free to check that out.
For this tutorial lets go ahead and create some embedded documents. So, connect to your MongoDB server and run the following command to insert some documents in the shapes collection.
> db.shapes.insertMany([ { "shape": "rectangle", "item": "Rect 1", "dim": { "length": 10, "breadth": 20, "uom": "cm" } }, { "shape": "rectangle", "item": "Rect 2", "dim": { "length": 20, "breadth": 30, "uom": "cm" } }, { "shape": "rectangle", "item": "Rect 3", "dim": { "length": 5, "breadth": 10, "uom": "cm" } }, { "shape": "square", "item": "Sq 1", "dim": { "side": 10.5, "uom": "cm" } }, { "shape": "square", "item": "Sq 2", "dim": { "side": 20, "uom": "cm" } }, { "shape": "square", "item": "Sq 3", "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.
Alright, let's start simple then progress to advance problems.
To find all the documents in the shapes collection that represents a rectangle we have to run the following command.
shapes
> db.shapes.find({ "shape": "rectangle" }).pretty(); { "_id" : ObjectId("5d175dffba3250e57f98faca"), "shape" : "rectangle", "item" : "Rect 1", "dim" : { "length" : 10, "breadth" : 20, "uom" : "cm" } } { "_id" : ObjectId("5d175dffba3250e57f98facb"), "shape" : "rectangle", "item" : "Rect 2", "dim" : { "length" : 20, "breadth" : 30, "uom" : "cm" } } { "_id" : ObjectId("5d175dffba3250e57f98facc"), "shape" : "rectangle", "item" : "Rect 3", "dim" : { "length" : 5, "breadth" : 10, "uom" : "cm" } }
Alright, we are now moving to embedded documents.
First we have to check the shape to be equal to square.
shape
square
Next, we have to check the side field which is inside the dim field. So, we have to use dim.side in our filter.
side
dim
dim.side
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.
$gte
And for the unit of measurement uom we have to check the dim.uom to be equal to cm.
uom
dim.uom
cm
Our query should look like the following.
> db.shapes.find({ "shape": "square", "dim.side": { $gte: 20 }, "dim.uom": "cm" }).pretty(); { "_id" : ObjectId("5d175dffba3250e57f98face"), "shape" : "square", "item" : "Sq 2", "dim" : { "side" : 20, "uom" : "cm" } }
For this we have to check the following.
> db.shapes.find({ "shape": "rectangle", "dim.length": { $gte: 10 }, "dim.breadth": { $gte: 20 }, "dim.uom": "cm" }).pretty(); { "_id" : ObjectId("5d175dffba3250e57f98faca"), "shape" : "rectangle", "item" : "Rect 1", "dim" : { "length" : 10, "breadth" : 20, "uom" : "cm" } } { "_id" : ObjectId("5d175dffba3250e57f98facb"), "shape" : "rectangle", "item" : "Rect 2", "dim" : { "length" : 20, "breadth" : 30, "uom" : "cm" } }