Computer scienceFundamentalsSQL and DatabasesDBMSMongoDBOperations and operatorsOperators

Logical Operators

5 minutes read

In this topic, we will talk about logical operators in MongoDB. Logical operators are operators that we use to connect two or more expressions. They can be helpful when you need to consider both conditions simultaneously or when you need to consider only one of many conditions. With the help of logical operators, you can easily filter documents while reading, updating, and deleting.

Logical operators in MongoDB

MongoDB has four common logical operators: AND, OR, NOT, and NOR. Their syntax is straightforward and intuitively understandable. Let's take a look at them in this table:

Operator Description
$and Checks if the value matches all the conditions.
$or Checks if the value matches at least one condition.
$not Checks if the value does not match the condition.
$nor Checks if the value fails all the query expressions.

Now let's talk about each of the operators and review the examples of their implementation.

AND and OR operators

First things first, let's talk about $and and $or logical operators. We use the $and operator to combine multiple conditions when we should take both of them into account. It has the following syntax:

{$and: [{condition1}, {condition2}, ..., {conditionN}]}

We can also write it without the $and operator. Just by listing:

{{condition1}, {condition2}, ..., {conditionN}}

The $or operator is used when we need to take into account at least one of several conditions. It has the same syntax as $and:

{$or: [{condition1}, {condition2}, ..., {conditionN}]}

Examples

Now it's time for examples. Let's create a collection named employees with such fields as name, company, salary and department. Then fill it with data:

[
  {id: 1, name: "John Smith", company: "Sell Good Inc.", salary: 10000, department: "marketing"},
  {id: 2, name: "Cora Proctor", company: "Fresh Start", salary: 8000, department: "IT"},
  {id: 3, name: "Gabriel Hoffman", company: "Star Assistance", salary: 10000, department: "IT"},
  {id: 4, name: "Stacy Bixler", company: "Sell Good Inc.", salary: 9000, department: "design"},
]

First of all, we will practice the $and operator. We should get the record about an employee who works in Sell Good Inc. in the Marketing department.

db.employees.find({$and: [{company: "Sell Good Inc."}, {department: "marketing"}]})

In this case, we will get only the first record that satisfies our conditions:

[
  {id: 1, name: "John Smith", company: "Sell Good Inc.", salary: 10000, department: "marketing"}
]

Now let's work with the $or operator. We will get information about an employee from the IT or Design departments using this command:

db.employees.find({$or: [{department: "IT"}, {department: "design"}]})

The result will show three employees that work in the IT or Design departments:

[
  {id: 2, name: "Cora Proctor", company: "Fresh Start", salary: 8000, department: "IT"},
  {id: 3, name: "Gabriel Hoffman", company: "Star Assistance", salary: 10000, department: "IT"},
  {id: 4, name: "Stacy Bixler", company: "Sell Good Inc.", salary: 9000, department: "design"}
]

NOT and NOR operators

Our next task is to learn $not and $nor logical operators that MongoDB has. The $not operator is used to get records that do not match the given condition. The $not can be used only with other operator expressions such as $eq, $gt and so on. It has such syntax:

{field: {$not: {expression}}}

The $nor operator is used to select only those documents that do not match all the conditions given in an array. The syntax of logical NOR in MongoDB is the following:

{$nor: [{condition1}, {condition2}, ..., {conditionN}]}

For a better understanding, below, we will review these logical operators with examples from the same employees table.

Let's get documents about an employee whose salary is not higher than 9000:

db.employees.find({salary: {$not: {$gt: 9000}}})

At that rate, we will get two records where the salary equals 8000 and 9000:

[
  {id: 2, name: "Cora Proctor", company: "Fresh Start", salary: 8000, department: "IT"},
  {id: 4, name: "Stacy Bixler", company: "Sell Good Inc.", salary: 9000, department: "design"}
]

Now we should practice the $nor operator. We will select the records about employees that do not work in Sell Good Inc., and whose salary is not 8000:

db.employees.find({$nor: [{company: "Sell Good Inc."}, {salary: 8000}]})

The result will give us only one person:

[
  {id: 3, name: "Gabriel Hoffman", company: "Star Assistance", salary: 10000, department: "IT"}
]

Conclusion

In this topic, you learned about logical operators in MongoDB. Feel free to use them in your practice; they will make your development process more convenient. There are some key facts from this topic:

  • MongoDB has four logical operators: $and, $or, $not, $nor.
  • $and can be represented by listing the conditions.
  • $not can be used only with other operator expressions.

Now let's put your knowledge to practice!

16 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo