codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

17 / 65 topics
12Querying Basics13Find Queries14Projection15Sorting and Limiting16Aggregation Framework17Operators in MongoDB18Text Search
Tutorials/MongoDB/Operators in MongoDB
🍃MongoDB

Operators in MongoDB

Updated 2026-04-20
3 min read

Operators in MongoDB

Introduction

Operators in MongoDB are symbols or keywords used in queries to specify conditions that documents must meet to be selected. They allow for complex querying and manipulation of data within collections. This tutorial will cover various types of operators, their syntax, real-world examples, and best practices.

Types of Operators

Comparison Operators

Comparison operators are used to compare the value of a field with a specified value.

  • $eq: Matches values that are equal to a specified value.

    db.collection.find({ age: { $eq: 30 } })
    
  • $ne: Matches all values that are not equal to a specified value.

    db.collection.find({ age: { $ne: 30 } })
    
  • $gt: Matches values greater than a specified value.

    db.collection.find({ age: { $gt: 30 } })
    
  • $gte: Matches values greater than or equal to a specified value.

    db.collection.find({ age: { $gte: 30 } })
    
  • $lt: Matches values less than a specified value.

    db.collection.find({ age: { $lt: 30 } })
    
  • $lte: Matches values less than or equal to a specified value.

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

Logical Operators

Logical operators are used to combine multiple conditions.

  • $and: Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.

    db.collection.find({
      $and: [
        { age: { $gt: 30 } },
        { name: "John" }
      ]
    })
    
  • $or: Joins query clauses with a logical OR returns all documents that match the conditions of either clause.

    db.collection.find({
      $or: [
        { age: { $gt: 30 } },
        { name: "John" }
      ]
    })
    
  • $not: Inverts the effect of a query expression and returns documents that do not match the query expression.

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

Element Operators

Element operators are used to check for the existence of a field or its value.

  • $exists: Matches documents that have the specified field.

    db.collection.find({ name: { $exists: true } })
    
  • $type: Selects documents if a field is of the specified type.

    db.collection.find({ age: { $type: "number" } })
    

Array Operators

Array operators are used to query arrays.

  • $in: Matches any of the values specified in an array.

    db.collection.find({ age: { $in: [30, 40, 50] } })
    
  • $nin: Matches none of the values specified in an array.

    db.collection.find({ age: { $nin: [30, 40, 50] } })
    
  • $all: Matches arrays that contain all elements specified in the query.

    db.collection.find({ tags: { $all: ["mongodb", "database"] } })
    

Evaluation Operators

Evaluation operators are used to match documents based on the result of an expression.

  • $expr: Allows the use of aggregation expressions within the query language.

    db.collection.find({
      $expr: {
        $gt: [{ $size: "$tags" }, 2]
      }
    })
    
  • $mod: Performs a modulo operation on the value of a field and selects documents with a specified result.

    db.collection.find({ age: { $mod: [10, 0] } }) // Matches ages divisible by 10
    

Best Practices

  1. Use Indexes: Ensure that fields used in queries have indexes to improve query performance.

  2. Avoid Complex Queries: Keep queries as simple as possible to enhance readability and maintainability.

  3. Leverage Aggregation Framework: For complex data processing, use the aggregation framework instead of nested queries.

  4. Test Queries: Always test your queries with sample data to ensure they return the expected results.

  5. Use $expr for Complex Conditions: When dealing with conditions that require calculations or transformations, use $expr.

Real-World Examples

Example 1: Finding Users by Age Range

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

This query finds all users whose age is between 20 and 30.

Example 2: Finding Documents with Specific Tags

db.articles.find({
  tags: { $all: ["mongodb", "database"] }
})

This query retrieves articles that have both "mongodb" and "database" in their tags array.

Example 3: Using Logical Operators to Combine Conditions

db.orders.find({
  $and: [
    { status: "shipped" },
    { amount: { $gt: 100 } }
  ]
})

This query finds all orders that are shipped and have an amount greater than 100.

Conclusion

Operators in MongoDB provide a powerful way to query and manipulate data. By understanding the different types of operators and their usage, you can write efficient and effective queries for your applications. Always consider performance implications and best practices when designing your queries.


PreviousAggregation FrameworkNext Text Search

Recommended Gear

Aggregation FrameworkText Search