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.
$eq, $ne, $gt, $gte, $lt, $lte, $in, $nin.
// Basic equality (implicit $eq)db.users.find({ age: 30 });db.users.find({ age: { $eq: 30 } }); // same// Comparisondb.users.find({ age: { $gt: 18 } });db.users.find({ age: { $gte: 18, $lte: 65 } });db.users.find({ status: { $ne: "banned" } });// IN / NOT INdb.users.find({ role: { $in: ["admin", "tutor"] } });db.users.find({ role: { $nin: ["banned", "suspended"] } });
$and, $or, $not, $nor.
// 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 } }// $ordb.users.find({ $or: [{ role: "admin" }, { tier: "premium" }] });// $notdb.users.find({ age: { $not: { $gte: 18 } } });// $nor (none match)db.users.find({ $nor: [{ role: "banned" }, { age: { $lt: 13 } }] });
$exists, $type.
// Has the field at alldb.users.find({ phone: { $exists: true } });db.users.find({ deletedAt: { $exists: false } });// Specific BSON typedb.users.find({ age: { $type: "int" } });db.users.find({ age: { $type: ["int", "long"] } });// Match documents where a field is null OR missingdb.users.find({ deletedAt: null }); // matches null AND missing
Querying arrays.
// Schema:// { tags: ["a", "b", "c"], scores: [88, 92, 75] }// Array contains a valuedb.users.find({ tags: "admin" });// Array contains ALL listed valuesdb.users.find({ tags: { $all: ["admin", "beta"] } });// Array lengthdb.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 elementsdb.students.find({"scores.subject": "math","scores.score": { $gte: 85 }}); // matches if ANY score is math AND ANY score is >=85, not necessarily same!
Pattern matching.
// Regex (case-sensitive)db.users.find({ name: /^Al/ });db.users.find({ name: { $regex: "^Al" } });// Case-insensitivedb.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" } });
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.