Learning MongoDB

0% completed

Previous
Next
Logical Operators

Logical operators in MongoDB are used to combine multiple query conditions. They allow you to create more complex queries by applying logical operations like AND, OR, NOT, and NOR to filter documents based on multiple criteria. Understanding how to use logical operators effectively is crucial for constructing flexible and powerful queries.

Logical Operators

OperatorDescription
$andJoins query clauses with a logical AND and returns all documents that match the conditions of both clauses.
$orJoins query clauses with a logical OR and returns all documents that match the conditions of at least one clause.
$notInverts the effect of a query expression and returns documents that do not match the query expression.
$norJoins query clauses with a logical NOR and returns all documents that fail to match both clauses.

Syntax for Using Logical Operators

The general syntax for using logical operators in MongoDB is:

db.collection.find( { <logicalOperator>: [ { <condition1> }, { <condition2> }, ... ] } )
  • logicalOperator: The logical operator to apply.
  • condition1, condition2, ...: The query conditions to combine.

Example Setup

First, let's insert some documents into the users collection to work with:

db.users.insertMany([ { name: "Alice", age: 25, email: "alice@example.com", scores: [85, 90, 88] }, { name: "Bob", age: 30, email: "bob@example.com", scores: [78, 82, 79] }, { name: "Charlie", age: 35, email: "charlie@example.com", scores: [92, 85, 90] }, { name: "David", age: 28, email: "david@example.com", scores: [88, 84, 89] }, { name: "Eve", age: 22, email: "eve@example.com", scores: [80, 86, 81] }, { name: "Frank", age: 30, email: "frank@example.com", scores: [75, 82, 78] }, { name: "Grace", age: 40, email: "grace@example.com", scores: [95, 91, 94] }, { name: "Heidi", age: 25, email: "heidi@example.com", scores: [85, 89, 87] } ])

This setup creates a users collection with various documents containing name, age, email, and scores fields. We will use these documents for demonstrating the logical operators in the subsequent sections.

$and Operator

The $and operator joins query clauses with a logical AND and returns all documents that match the conditions of both clauses.

Examples

  1. Query for Age Greater Than 25 AND Less Than 35

    db.users.find({ $and: [ { age: { $gt: 25 } }, { age: { $lt: 35 } } ] })

    This command retrieves all documents where the age field is greater than 25 and less than 35.

  2. Query for Name "Alice" AND Age 25

    db.users.find({ $and: [ { name: "Alice" }, { age: 25 } ] })

    This command retrieves all documents where the name is "Alice" and the age is 25.

$or Operator

The $or operator joins query clauses with a logical OR and returns all documents that match the conditions of at least one clause.

Examples

  1. Query for Age Less Than 25 OR Greater Than 35

    db.users.find({ $or: [ { age: { $lt: 25 } }, { age: { $gt: 35 } } ] })

    This command retrieves all documents where the age field is less than 25 or greater than 35.

  2. Query for Name "Alice" OR Age 30

    db.users.find({ $or: [ { name: "Alice" }, { age: 30 } ] })

    This command retrieves all documents where the name is "Alice" or the age is 30.

$not Operator

The $not operator inverts the effect of a query expression and returns documents that do not match the query expression.

Examples

  1. Query for Age NOT Equal to 30

    db.users.find({ age: { $not: { $eq: 30 } } })

    This command retrieves all documents where the age field is not equal to 30.

  2. Query for Name NOT "Alice"

    db.users.find({ name: { $not: { $eq: "Alice" } } })

    This command retrieves all documents where the name is not "Alice".

$nor Operator

The $nor operator joins query clauses with a logical NOR and returns all documents that fail to match both clauses.

Examples

  1. Query for Age NOT Less Than 25 NOR Greater Than 35

    db.users.find({ $nor: [ { age: { $lt: 25 } }, { age: { $gt: 35 } } ] })

    This command retrieves all documents where the age field is neither less than 25 nor greater than 35.

  2. Query for Name NOT "Alice" NOR Age 30

    db.users.find({ $nor: [ { name: "Alice" }, { age: 30 } ] })

    This command retrieves all documents where the name is not "Alice" and the age is not 30.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next