Learning MongoDB

0% completed

Previous
Next
Comparison Operators

Comparison operators in MongoDB are used to compare the values of fields in documents against specified values. These operators allow you to filter documents based on conditions such as equality, inequality, greater than, less than, and membership in a set. Understanding how to use comparison operators effectively is essential for constructing powerful and flexible queries.

Comparison Operators

Here, we have list down all comparison operators.

OperatorDescription
$eqMatches values that are equal to a specified value.
$neMatches values that are not equal to a specified value.
$gtMatches values that are greater than a specified value.
$gteMatches values that are greater than or equal to a specified value.
$ltMatches values that are less than a specified value.
$lteMatches values that are less than or equal to a specified value.
$inMatches any of the values specified in an array.
$ninMatches none of the values specified in an array.

Syntax for Using Comparison Operators

The general syntax for using comparison operators in MongoDB is:

db.collection.find( { <field>: { <operator>: <value> } } )
  • field: The field to compare.
  • operator: The comparison operator.
  • value: The value to compare against.

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, and email fields. We will use these documents for demonstrating the comparison operators in the subsequent sections.

$eq Operator

The $eq operator matches documents where the value of a field is equal to the specified value.

Examples

  1. Query for a Specific Age

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

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

  2. Query for a Specific Name

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

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

$ne Operator

The $ne operator matches documents where the value of a field is not equal to the specified value.

Examples

  1. Query for Age Not Equal to 30

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

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

  2. Query for Name Not Equal to "Alice"

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

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

$gt Operator

The $gt operator matches documents where the value of a field is greater than the specified value.

Examples

  1. Query for Age Greater Than 30

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

    This command retrieves all documents where the age field is greater than 30.

  2. Query for Scores Greater Than 85

    db.users.find({ scores: { $gt: 85 } })

    This command retrieves all documents where any value in the scores array is greater than 85.

$gte Operator

The $gte operator matches documents where the value of a field is greater than or equal to the specified value.

Examples

  1. Query for Age Greater Than or Equal to 30

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

    This command retrieves all documents where the age field is greater than or equal to 30.

  2. Query for Scores Greater Than or Equal to 85

    db.users.find({ scores: { $gte: 85 } })

    This command retrieves all documents where any value in the scores array is greater than or equal to 85.

$lt Operator

The $lt operator matches documents where the value of a field is less than the specified value.

Examples

  1. Query for Age Less Than 30

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

    This command retrieves all documents where the age field is less than 30.

  2. Query for Scores Less Than 85

    db.users.find({ scores: { $lt: 85 } })

    This command retrieves all documents where any value in the scores array is less than 85.

$lte Operator

The $lte operator matches documents where the value of a field is less than or equal to the specified value.

Examples

  1. Query for Age Less Than or Equal to 30

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

    This command retrieves all documents where the age field is less than or equal to 30.

  2. Query for Scores Less Than or Equal to 85

    db.users.find({ scores: { $lte: 85 } })

    This command retrieves all documents where any value in the scores array is less than or equal to 85.

$in Operator

The $in operator matches documents where the value of a field matches any of the values specified in an array.

Examples

  1. Query for Age in a List

    db.users.find({ age: { $in: [25, 30, 35] } })

    This command retrieves all documents where the age field is either 25, 30, or 35.

  2. Query for Names in a List

    db.users.find({ name: { $in: ["Alice", "Bob"] } })

    This command retrieves all documents where the name field is either "Alice" or "Bob".

$nin Operator

The $nin operator matches documents where the value of a field does not match any of the values specified in an array.

Examples

  1. Query for Age Not in a List

    db.users.find({ age: { $nin: [25, 30, 35] } })

    This command retrieves all documents where the age field is not 25, 30, or 35.

  2. Query for Names Not in a List

    db.users.find({ name: { $nin: ["Alice", "Bob"] } })

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

.....

.....

.....

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