Logical Operator in MongoDB

By | September 23, 2022

A logical operator is a word or a function that can be used to compare two or more expressions so that the compounded result will return the boolean value. MongoDB supports the standard set of four Logical Operators that we already know in our programming language like Java. Following are the available logical operators in MongoDB.

OperatorDescription
$andThe $and operator match all the query statements specified.
$orThe $or operator matches at least one query statement specified.
$norThe $nor operator fails to match non of the query statement specified.
$notThe $not operator ignores or negates the query statement requirement.
Table: Logical Operators in MongoDB

Logical operator query syntax

The syntax for $and, $or and $nor operators are the same. These operators accept an array of expressions like in the syntax given below:

{<operator>: [{expression1}, {expression2}, ...]}

The syntax for $not operator is different than others. It accepts only one expression which needs to be negated. The array syntax is not necessary for this operator. See the syntax below for $not operator:

{ fieldName: { $not: { operator-expression } } }

 

$and operator in MongoDB

The $and operator in MongoDB is used by default when an operator is not specified. It performs a logical AND operation on an array of one or more expressions that matches all the query expressions that are specified.

The default $and operator example:

db.employee.find({"salary": 50000, "department":"Engineering"})

The above query returns all the employees from Engineering department whose salary is 50000.

Here, we haven’t used $and operator but it still worked as $and operator.

Hence, it is confirmed that when we don’t define an operator in the query then the default operator would be $and.

$and operator example:

db.employee.find({"$and":[{"salary":50000},{"department":"Engineering"}]})

This query also returns the same data as the earlier query which was without $and operator defined explicitly.

$or operator in MongoDB

The $or operator performs the logical OR operation on an array of one or more expressions and selects all the documents that satisfy at least one query expression specified.

 Example:

db.employee.find({"$or":[{"salary":50000},{"department":"Engineering"}]})

This query returns the data that matches either salary 50000 or department Engineering.

$nor operator in MongoDB

The $nor operator performs the logical operator NOR operation on an array of one or more query expressions and selects the documents that fail to match non of the query statement specified.

Example:

db.employee.find({"$nor":[{"salary":50000},{"department":"Engineering"}]})

This query ignores both the condition. This basically means that it returns the employee document, neither the salary is 50000 nor the department is Engineering.

$not operator in MongoDB

The $not operator ignores or negates the query statement requirement. It performs a logical NOT operation with the given operator-expression and selects the documents that do not match the given operator expression.

Example:

db.employee.find({"salary": {"$not":{"$gt":50001}}})

The above query returns the documents whose salary is not greater than 50001 from the employee collection

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments