MongoDB Query Documents

Executing MongoDB commands to retrieve specific data from documents in the database collections using query operations.

Sample Dataset

db.products.insertMany([
  {
    "title": "OnePlus Nord 3",
    "brand": "oneplus",
    "category": "mobile",
    "price": 33999,
    "qty": 25,
    "display": 6.74,
    "storage": {
      "ram": 8,
      "internal": 128
    },
    "color": ["green", "gray"]
  },
  {
    "title": "OnePlus Nord CE 3",
    "brand": "oneplus",
    "category": "mobile",
    "price": 26999,
    "qty": 10,
    "display": 6.7,
    "storage": {
      "ram": 12,
      "internal": 256
    },
    "color": ["aqua"]
  },
  {
    "title": "Samsung Galaxy M34",
    "brand": "samsung",
    "category": "mobile",
    "price": 18999,
    "qty": 72,
    "display": 6.4,
    "storage": {
      "ram": 6,
      "internal": 128
    },
    "color": ["black", "dark blue", "blue"]
  },
  {
    "title": "Samsung Galaxy M14",
    "brand": "samsung",
    "category": "mobile",
    "price": 14990,
    "qty": 8,
    "display": 6.58,
    "storage": {
      "ram": 4,
      "internal": 128
    },
    "color": ["silver", "black"]
  },
  {
    "title": "realme narzo N53",
    "brand": "realme",
    "category": "mobile",
    "price": 8999,
    "qty": 10,
    "display": 6.72,
    "storage": {
      "ram": 6,
      "internal": 128
    },
    "color": ["gold", "black"]
  },
  {
    "title": "Vivo T2x",
    "brand": "vivo",
    "category": "mobile",
    "price": 13999,
    "qty": 4,
    "display": 6.58,
    "storage": {
      "ram": 6,
      "internal": 128
    },
    "color": ["silver", "black"]
  },
  {
    "title": "Redmi Note 12",
    "brand": "xiaomi",
    "category": "mobile",
    "price": 16999,
    "qty": 25,
    "display": 6.67,
    "storage": {
      "ram": 6,
      "internal": 128
    },
    "color": ["black", "orange"]
  },
  {
    "title": "Xiaomi Pad 6",
    "brand": "xiaomi",
    "category": "tab",
    "price": 28999,
    "qty": 45,
    "display": 11,
    "storage": {
      "ram": 8,
      "internal": 256
    },
    "color": ["grey", "black", "blue"]
  },
  {
    "title": "Apple iPadĀ Air",
    "brand": "apple",
    "category": "tab",
    "price": 68400,
    "qty": 2,
    "display": 10.9,
    "storage": {
      "ram": 8,
      "internal": 64
    },
    "color": ["grey", "pink"]
  }
])

Select All Documents in a Collection

To select all documents in the collection, pass an empty document as the query filter parameter in find function.

db.products.find({})

This operation uses a filter predicate of {}, which corresponds to the following SQL statement:

SELECT * FROM products

Specify Equality Condition

The following example selects from the products collection all documents where the brand equals "oneplus":

db.products.find({"brand":"oneplus"})

This operation uses a filter predicate of { brand: "oneplus" }, which corresponds to the following SQL statement:

SELECT * FROM products WHERE brand = "oneplus"

Specify Conditions Using Query Operators

The following example retrieves all documents from the products collection where brand equals either "apple" or "realme":

db.products.find({"brand":{$in:["apple","realme"]}})

The operation uses a filter predicate of {"brand":{$in:["apple","realme"]}}, which corresponds to the following SQL statement:

SELECT * FROM products WHERE brand in ("apple", "realme")

Specify AND Conditions

The following example retrieves all documents in the products collection where the category equals "mobile" and qty is less than ($lt) 20:

db.products.find({
    "category":"mobile",
    "qty":{$lt:20}
})

The operation uses a filter predicate of { "category":"mobile", "qty":{$lt:20} } which corresponds to the following SQL statement:

SELECT * FROM products WHERE category="mobile" AND qty<20;

Specify OR Conditions

The following example retrieves all documents in the collection where the category equals "mobile" or qty is less than ($lt) 20:

db.products.find({
$or:[
    {"category":"mobile"},
    {"qty":{$lt:20}}
  ]
})

The operation uses a filter predicate of { $or:[ {"category":"mobile"}, {"qty":{$lt:20}} ] } which corresponds to the following SQL statement:

SELECT * FROM products WHERE category="mobile" or qty<20;

Specify AND as well as OR Conditions

The following example selects all documents in the collection where the category equals "mobile" and either qty is less than ($lt) 10 or title starts with the character r:

db.products.find({
  "category":"mobile",
  $or:[
    {"qty":{$lt:10}},
    {"title":/^r/}
  ]
})

Query can be rewrite using $regex

db.products.find({
  "category":"mobile",
  $or:[
    {"qty":{$lt:10}},
    {"title":{$regex:'^r'}}
  ]
})

The operation uses a filter predicate of:


  {
    category: 'mobile',
    $or: [
      { qty: { $lt: 10 } }, { title: { $regex: '^r' } }
    ]
  }

which corresponds to the following SQL statement:

SELECT * FROM products WHERE category = "mobile" AND ( qty < 10 OR item LIKE "r%")