CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Mongo DB > Logical Operator in MongoDB

Logical Operator in MongoDB

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X
Logical Operators in MongoDB

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

Related Posts:

  • Control Statements in Java
  • java.lang.Boolean class in Java
  • Expressions in Java
  • Boolean Data Type in Java
  • Concat Column in MySQL
  • Java Regex Tutorial: Pattern, Matcher, and Practical…
Tags:mongodb
Was this article helpful?
โ† Previous ArticleComparison Operator in MongoDB
Next Article โ†’How Java Is Platform Independent?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap