Skip to content

#11 Leverage the Power of Operators in MongoBD

MongoDB offers powerful querying capabilities through its versatile set of operators. These operators provide developers with fine-grained control over data retrieval and manipulation, making MongoDB a preferred choice for many modern applications.

In this article, I will introduce and explain main operators, exploring their functionality and providing practical examples to demonstrate their usage.

Comparison Operators

  • $eq: Matches values that are equal to a specified value. (A == B)
  • $gt: Matches values that are greater than a specified value. (A > B)
  • $gte: Matches values that are greater than or equal to a specified value. (A >= B)
  • $lt: Matches values that are less than a specified value. (A < B)
  • $lte: Matches values that are less than or equal to a specified value. (A <= B)

Example 1: Find employees whose salary are less than or equal to 60000

 > db.personnel.find({ "salary": { "$lte": 60000 } },{name:1, salary:1, department:1})

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c4'),
    name: 'John Doe',
    department: 'Engineering',
    salary: 60000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c5'),
    name: 'Jane Smith',
    department: 'Marketing',
    salary: 55000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c9'),
    name: 'Sarah Lee',
    department: 'Customer Support',
    salary: 50000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cc'),
    name: 'Daniel Thompson',
    department: 'Product Management',
    salary: 58000
  }
]

Example 2: Find employees whose age are older than 33 and salary are above 65000

> db.personnel.find({"age":{"$gt":33}, "salary": { "$gt": 65000 } },{name:1, age:1, salary:1, department:1})

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    age: 35,
    department: 'Finance',
    salary: 70000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    age: 40,
    department: 'Operations',
    salary: 75000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21ca'),
    name: 'Alex Garcia',
    age: 34,
    department: 'Sales',
    salary: 67000
  }
]

Logical Operators

  • $and: Joins query clauses with a logical AND and returns all documents that match the conditions of both clauses.
  • $or: Joins query clauses with a logical OR and returns all documents that match the conditions of any of the clauses.
  • $not: Inverts the effect of a query expression and returns documents that do NOT match the query expression.

Example 3: Find employees whose age are older than 35 and salary are above 68000

> db.personnel.find({"$and": [ { "age": { "$gt": 35 } }, { "salary": { "$gt": 68000 } } ]},{name:1, name:1, salary:1})

{
  _id: ObjectId('66053f848ccb6407b4ba21c8'),
  name: 'David Wilson',
  salary: 75000
}

Example 3: Find employees who are not in Finance department or Engineering or Marketing department

> db.personnel.find({"department": {"$not": {"$in": ["Finance", "Engineering", "Marketing"]}}}, {"name": 1,"department": 1})

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c7'),
    name: 'Emily Brown',
    department: 'Human Resources'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    department: 'Operations'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c9'),
    name: 'Sarah Lee',
    department: 'Customer Support'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21ca'),
    name: 'Alex Garcia',
    department: 'Sales'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cb'),
    name: 'Michelle Martinez',
    department: 'Research and Development'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cc'),
    name: 'Daniel Thompson',
    department: 'Product Management'
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cd'),
    name: 'Amanda Hall',
    department: 'Quality Assurance'
  }
]

Conditional Expression Operators

  • $cond: Evaluates a boolean expression and returns one of two specified results depending on whether the expression evaluates to true or false.
  • $ifNull: Returns the value of the first expression if it is not null, otherwise, it returns the second expression.
  • $switch: Evaluates a series of case expressions and returns the corresponding result expression for the first case expression that evaluates to true. If no case expression evaluates to true, $switch returns the default result.

Others

There are many other types of operators such as Array Expression Operators, Date Expression Operators, Arithmetic Operators, Text Expression Operators, and so on. I will show you their meanings and examples in other articles.

If you are interested in the operators, please read the MongoDB Documentation. This page will be very helpful.