close
The Wayback Machine - https://web.archive.org/web/20250101183549/https://www.geeksforgeeks.org/mongodb-logical-query-operators/
Open In App

MongoDB – Logical Query Operators

Last Updated : 10 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Logical query operators in MongoDB are fundamental tools used to combine or modify the results of queries using logical conditions. These operators empower developers to create complex queries by combining multiple conditions and enabling precise filtering of documents based on various criteria.

In this article, We will learn about the Logical Query Operator in MongoDB by understanding various operators with examples and so on.

Logical Query Operator in MongoDB

  • Logical query operators in MongoDB are used to combine or modify the results of queries using logical conditions.
  • These operators allow us to create complex queries by combining multiple conditions.
  • Logical operators allow for the creation of complex queries that combine multiple conditions. This enables precise filtering of documents based on various criteria.
  • Logical operators can make queries more readable by clearly separating different conditions and their logical relationships.

Logical query operators like $and and $or allow for more flexible database queries. To fully leverage these operators in full-stack applications, the Full Stack Development with Node JS course provides practical examples on building powerful MongoDB queries with Node.js

Operators

The following table contains the various Logical Query Operators are as follows:

It Joins query clauses with a logical OR. Returns documents that match any of the conditions.

Operator Description Syntax
$and It Joins query clauses with a logical AND. Returns documents that match all the conditions. { $and: [ { <expression1> }, { <expression2> }, ... ] }
$or { $or: [ { <expression1> }, { <expression2> }, ... ] }
$nor It Joins query clauses with a logical NOR. Returns documents that fail to match all the conditions. { $nor: [ { <expression1> }, { <expression2> }, ... ] }
$not It Inverts the effect of a query expression. Returns documents that do not match the query expression. { <field>: { $not: { <operator-expression> } } }

Exampes of Logical Query Operators

This table provides a clear and concise

In the following examples, we are working with:

Database: GeeksforGeeks
Collection: contributor
Document: three documents that contain the details of the contributors in the form of field-value pairs.

Output

BERJAYA

Example 1: Matching values using $and operator

In this example, we are retrieving only those employee’s documents whose branch is CSE and joiningYear is 2018.

db.contributor.find({$and: [{branch: "CSE"}, {joiningYear: 2018}]}).pretty()

Output

BERJAYA

Example 2: Matching values using $nor operator

In this example, we are retrieving only those employee’s documents whose salary is not 3000 and whose branch is not ECE.

 db.contributor.find({$nor: [{salary: 3000}, {branch: "ECE"}]}).pretty()

Output

BERJAYA

Example 3: Matching values using $or operator

In this example, we are retrieving only those employee’s documents whose branch is ECE or joiningYear is 2017.

db.contributor.find({$or: [{branch: "ECE"}, {joiningYear: 2017}]}).pretty()

Output

BERJAYA

Example 4: Matching values using $not operator

In this example, we are retrieving only those employee’s documents whose salary is not greater than 2000.

db.contributor.find({salary: {$not: {$gt: 2000}}}).pretty()

Output

Matching-values-using--not-operator

Conclusion

Logical query operators in MongoDB are powerful tools for constructing complex queries that combine multiple conditions. They enable precise filtering of documents, enhancing the readability and maintainability of queries.

FAQs on Logical Query Operators

What are the logical query operators in MongoDB?

Logical query operators in MongoDB are used to combine or modify query conditions to create complex queries. The primary logical operators are $and, $or, $nor, and $not, which enable precise filtering of documents.

What is the query operator in MongoDB?

A query operator in MongoDB is a special keyword used to specify conditions in queries. These operators include comparison operators, logical operators, and element operators, enhancing the querying capabilities beyond simple equality checks.

Is it possible to nest logical query operators in MongoDB queries?

Yes, logical query operators can be nested in MongoDB queries to form complex and precise query conditions. Nesting allows combining multiple logical operators within a single query for intricate filtering logic.


Next Article

Similar Reads

three90RightbarBannerImg