MongoDB - Query Embedded Document

MongoDB

mongodb logo - tutorial - dyclassroom

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.

Embedded or Nested documents

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.

Find all rectangles

To find all the documents in the shapes collection that represents a rectangle we have to run the following command.

> 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"
  }
}

Find squares with sides at least 20 cm

Alright, we are now moving to embedded documents.

First we have to check the shape to be equal to 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.

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.

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"
  }
}

Find all rectangles having length at least 10 cm and breadth at least 20 cm

For this we have to check the following.

  • 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",
  "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"
  }
}

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT