Specify Equality Condition
The equality operator($eq) is used to match the documents where the value of the field is equal to the specified value. In other words, the $eq operator is used to specify the equality condition.
Example:
Retrieve and display all documents from the ‘article’ collection where the author is “devil”.
db.article.find({author:{$eq:"devil"}}).pretty()
Output:
[
{
_id: ObjectId('60095b8a3fc110f90873ce2c'),
title: 'Tree',
author: 'devil',
level: 'high',
length: 1000,
example: 10
},
{
_id: ObjectId('60095b8d3fc110f90873ce2e'),
title: 'Segment Tree',
author: 'devil',
level: 'very high',
length: 500,
example: 20
}
]
In the above Query, We have display the documents that matches the filter query(i.e., {author : {$eq : “devil”}}) from the article collection.
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.