CTCaliberTek
Course: MongoDB: From Documents to Production, Step by Step · Lesson 3 of 8Log in to track your progress and earn a certificate when you finish the course.
Data Engineering & Analytics

MongoDB Module 3: Query Operators, Arrays, Nested Fields, and Pagination

Published October 8, 2026·Updated October 29, 2026·10 min read
Like SaveLog in to like, save, and track progress.

Introduction

Real questions need more than equality filters. MongoDB's query operators cover ranges, sets, logic, text patterns, arrays, and nested objects. This module builds an orders collection and queries it in the ways you will need most.

Prerequisites

  • Module 2 completed

Step-by-Step Guide

Step 1: Create sample orders

Each order embeds a customer object and an array of line items, a typical MongoDB shape.

db.orders.insertMany([
  { orderId: 1, status: "shipped", placedAt: ISODate("2026-09-01"),
    customer: { name: "Asha", city: "Pune" },
    items: [ { sku: "A100", qty: 2, price: 4.5 }, { sku: "B200", qty: 10, price: 1.2 } ] },
  { orderId: 2, status: "pending", placedAt: ISODate("2026-09-03"),
    customer: { name: "Ravi", city: "Delhi" },
    items: [ { sku: "C300", qty: 1, price: 24.0 } ] },
  { orderId: 3, status: "shipped", placedAt: ISODate("2026-09-05"),
    customer: { name: "Meera", city: "Pune" },
    items: [ { sku: "A100", qty: 5, price: 4.5 }, { sku: "C300", qty: 2, price: 24.0 } ] }
])

Step 2: Use comparison and logical operators

Comparison operators are $eq, $ne, $gt, $gte, $lt, $lte, $in, and $nin. Combine conditions with $and, $or, and $not. Several conditions in one filter object are an implicit AND.

db.orders.find({ status: { $in: ["shipped", "pending"] } })
db.orders.find({ placedAt: { $gte: ISODate("2026-09-02"), $lt: ISODate("2026-09-06") } })
db.orders.find({ $or: [ { status: "pending" }, { "customer.city": "Delhi" } ] })

Step 3: Query nested fields with dot notation

Reach inside embedded objects by joining field names with a dot, in quotes. You can also use dot notation to update nested fields.

db.orders.find({ "customer.city": "Pune" })
db.orders.updateOne({ orderId: 2 }, { $set: { "customer.city": "New Delhi" } })

Step 4: Query arrays

Matching a value inside an array works like equality. Use $elemMatch when several conditions must be true for the same array element, because separate conditions can otherwise match different elements. $size matches array length, and $all requires several values.

db.orders.find({ "items.sku": "A100" })

// one item with sku A100 AND qty at least 5
db.orders.find({ items: { $elemMatch: { sku: "A100", qty: { $gte: 5 } } } })

db.orders.find({ items: { $size: 2 } })

Step 5: Paginate and match text

skip and limit page through results, but a large skip is slow because MongoDB still walks past the skipped documents. For deep paging use range-based pagination: remember the last key you saw and ask for the next ones after it. For pattern matching, $regex works, though case-insensitive and unanchored patterns cannot use indexes well. Use text or Atlas Search indexes for real search.

// offset pagination (fine for small pages)
db.orders.find().sort({ placedAt: -1 }).skip(20).limit(10)

// range pagination (scales better)
db.orders.find({ placedAt: { $lt: ISODate("2026-09-05") } }).sort({ placedAt: -1 }).limit(10)

db.orders.find({ "customer.name": { $regex: "^A", $options: "i" } })

FAQs

Why does my array query match the wrong documents?
Separate conditions on array fields can be satisfied by different elements. Use $elemMatch to require that one element meets all the conditions together.

Summary

Use comparison and logical operators for filters, dot notation for nested fields, $elemMatch for multi-condition array matches, range-based pagination for large result sets, and a search index instead of regex for real text search.

Related tutorials

Comments

Log in or create a free account to join the discussion.

  • No comments yet. Be the first to comment.