Specify AND as well as OR Conditions
Example:
Retrieve articles that are either authored by “Aditya” and have a level of “basic”, or authored by “Rakesh” with a level of “medium”.
db.article.find({
$or: [
{ level: "basic", author: "Aditya" },
{ $and: [
{ level: "medium" },
{ author: "Rakesh" }
]
}
]
}).pretty()
Output:
[
{
_id: ObjectId('6009585d35cce6b7b8f087f1'),
title: 'Math',
author: 'Aditya',
level: 'basic',
length: 230,
example: 11
},
{
_id: ObjectId('60095b8a3fc110f90873ce29'),
title: 'Array',
author: 'Aditya',
level: 'basic',
length: 200,
example: 5
},
{
_id: ObjectId('60095b8a3fc110f90873ce2b'),
title: 'Queue',
author: 'Rakesh',
level: 'medium',
length: 350,
example: 2
}
]
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