0PricingLogin
MongoDB Academy · Lesson

Logical Operators: $and, $or, $nor, $not

Learners will combine multiple conditions with logical operators to express compound filter logic.

Why Logical Operators Are Needed

Most query filters involve conditions on multiple fields, and not all of them should be combined with a simple AND. Sometimes you want documents that meet any of several conditions, or you want to exclude documents matching a pattern. MongoDB's logical operators$and, $or, $nor, and $not—give you full boolean control over your filter conditions.

Understanding when to use each operator—and knowing the implicit AND shortcut—makes your queries more readable and performant.

Implicit AND: The Default

When you list multiple field conditions in a single filter object, MongoDB applies them as an implicit AND—all conditions must be true for a document to match. This is the default and most common case.

The implicit AND is both more concise and slightly more efficient than the explicit $and operator because MongoDB can optimize field-level conditions independently. Use implicit AND whenever your conditions target different fields with no ambiguity.

// Implicit AND - all three conditions must be true
db.users.find({
  age: { $gte: 18 },
  active: true,
  role: 'user'
});
// Equivalent to: age >= 18 AND active = true AND role = 'user'

// Same with explicit $and (more verbose, same result)
db.users.find({
  $and: [
    { age: { $gte: 18 } },
    { active: true },
    { role: 'user' }
  ]
});

All lessons in this course

  1. Comparison Operators: $eq, $gt, $lt, $in
  2. Logical Operators: $and, $or, $nor, $not
  3. Element Operators and Type Checks
  4. Regex Queries and Pattern Matching
← Back to MongoDB Academy