Specify AND Conditions
$and operator comes under the type of MongoDB logical operator which perform logical AND operation on the array of one or more expressions and select or retrieve only those documents that match all the given expression in the array.
Example:
db.article.find({$and:[{level:{$eq:"high"}},{level:{$exists : "true"}}]}).pretty()
Output:
[
{
_id: ObjectId('60095b8a3fc110f90873ce2c'),
title: 'Tree',
author: 'devil',
level: 'high',
length: 1000,
example: 10
},
{
_id: ObjectId('60095b8a3fc110f90873ce2d'),
title: 'Graph',
author: 'Aditya',
level: 'high',
length: 1500,
example: 15
}
]
In the above Query, We have using $and operator and given two condition which are highlighted following
- and operator: {$and : [ first condition, second condition]}
- first condition(level == “high”): { level : {$eq : “high”}}
- second condition: {level : {$exists : “true”}}
What is a MongoDB Query?
MongoDB Query is a fundamental aspect of MongoDB that allows users to fetch data from the database. Similar to SQL queries in traditional databases, MongoDB queries provide simplicity and flexibility in retrieving specific data based on certain criteria or conditions.
In this article, We will learn about the MongoDB Query in detail by performing various Queries on document of MongoDB collection and so on.
Contact Us