█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/MongoDB/Query Operators
45 minBeginner

Query Operators

After this lesson, you will be able to: Use query operators: comparison ($eq, $gt, $lt, $in), logical ($and, $or, $not), element ($exists, $type), array ($elemMatch, $size).

Mongo's query language is a JSON-document filter. Once you see the operator categories, building queries is fast.

Prerequisites:CRUD Operations

Comparison operators

$eq, $ne, $gt, $gte, $lt, $lte, $in, $nin.

tsx
// Basic equality (implicit $eq)
db.users.find({ age: 30 });
db.users.find({ age: { $eq: 30 } }); // same
// Comparison
db.users.find({ age: { $gt: 18 } });
db.users.find({ age: { $gte: 18, $lte: 65 } });
db.users.find({ status: { $ne: "banned" } });
// IN / NOT IN
db.users.find({ role: { $in: ["admin", "tutor"] } });
db.users.find({ role: { $nin: ["banned", "suspended"] } });

Logical operators

$and, $or, $not, $nor.

tsx
// Implicit AND (multiple fields at same level)
db.users.find({ age: { $gte: 18 }, role: "tutor" });
// Explicit $and (when same field needs multiple conditions)
db.users.find({ $and: [{ age: { $gte: 18 } }, { age: { $lte: 65 } }] });
// Same as: { age: { $gte: 18, $lte: 65 } }
// $or
db.users.find({ $or: [{ role: "admin" }, { tier: "premium" }] });
// $not
db.users.find({ age: { $not: { $gte: 18 } } });
// $nor (none match)
db.users.find({ $nor: [{ role: "banned" }, { age: { $lt: 13 } }] });

Element + type operators

$exists, $type.

tsx
// Has the field at all
db.users.find({ phone: { $exists: true } });
db.users.find({ deletedAt: { $exists: false } });
// Specific BSON type
db.users.find({ age: { $type: "int" } });
db.users.find({ age: { $type: ["int", "long"] } });
// Match documents where a field is null OR missing
db.users.find({ deletedAt: null }); // matches null AND missing

Array operators

Querying arrays.

tsx
// Schema:
// { tags: ["a", "b", "c"], scores: [88, 92, 75] }
// Array contains a value
db.users.find({ tags: "admin" });
// Array contains ALL listed values
db.users.find({ tags: { $all: ["admin", "beta"] } });
// Array length
db.users.find({ tags: { $size: 3 } });
// Match an element by criteria ($elemMatch, for arrays of subdocuments)
// { scores: [{ subject: "math", score: 90 }, { subject: "english", score: 70 }] }
db.students.find({
scores: { $elemMatch: { subject: "math", score: { $gte: 85 } } }
});
// Without $elemMatch, separate conditions might match different elements
db.students.find({
"scores.subject": "math",
"scores.score": { $gte: 85 }
}); // matches if ANY score is math AND ANY score is >=85, not necessarily same!

Regex + text search

Pattern matching.

tsx
// Regex (case-sensitive)
db.users.find({ name: /^Al/ });
db.users.find({ name: { $regex: "^Al" } });
// Case-insensitive
db.users.find({ name: { $regex: "^al", $options: "i" } });
// Text search (requires a text index)
db.posts.createIndex({ title: "text", body: "text" });
db.posts.find({ $text: { $search: "mongodb tutorial" } });

Common mistakes

Using multiple array conditions without $elemMatch, matches across different elements. Anchored regex without index, slow. With anchor + index, fast (acts like prefix). $exists: false to find 'null' fields, also matches missing entirely. Often what you want, sometimes not. $or that could be expressed as $in, $in is faster.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←CRUD Operations
Back to MongoDB
Aggregation Pipeline→