Query Operator's
MongoDB Blog: Query Operators ($gt,$lt,$in,$or,)
Introduction:-
In MongoDB, query operators make it easy to filter, match, and retrieve specific documents from a collection. They enhance the power of find()
by allowing us to compare values, check for inclusion, perform logical operations.
$gt
, $lt
, $gte
, $lte
, $eq
, $ne
$or
, $and
, $not
, $nor
$exists
, $type
$in
, $nin
, $all
, $size
1. Comparison Operators:-
In MongoDB, comparison operators are used within queries to filter documents based on how field values relate to specific criteria, allowing you to retrieve data that matches exact or relative conditions. Common comparison operators include $eq
(equal to), $ne
(not equal to), $gt
(greater than), $gte
(greater than or equal to), $lt
(less than), and $lte
(less than or equal to).
#) $gt
(Greater Than)
In MongoDB, the $gt
operator, which stands for “greater than,” is used to filter documents where the value of a specified field is greater than a given number. It is a comparison operator commonly used in queries to retrieve data that exceeds a certain threshold, such as finding users above a specific age, products priced higher than a limit, or scores greater than a target.
Find documents where age
is greater than 25:
{ "age": { "$gt": 25 } }
#) $lt
(Less Than)
In MongoDB, the $lt
operator, meaning “less than,” is used to filter documents where the value of a specified field is lower than a given number. It helps in retrieving data that falls below a specific threshold, such as finding products priced under a certain amount, students scoring less than a target score, or users younger than a specific age. $lt (score < 80) query on mongodb compass
$in (city in ["New York", "Miami"]) query on mongodb compass
{ "city": { "$in": ["New York", "Miami"] } }
$or (age < 20 OR score > 90)
In MongoDB, the $or
operator is used to retrieve documents that satisfy at least one of multiple conditions, allowing you to combine different filters in a single query. It takes an array of conditions, and if any one of them is true, the document is returned.
{ "$or": [ { "age": { "$lt": 20 } }, { "score": { "$gt": 90 } } ]}
2. Logical Operators
In MongoDB, logical operators are used to combine multiple conditions within queries, enabling more complex and flexible filtering of documents. The most commonly used logical operators include $and
, $or
, $not
, and $nor
. The $and
operator returns documents that satisfy all specified conditions, while $or
returns documents that meet at least one of the given conditions. The $not
operator inverts a condition, selecting documents that do not match the specified expression, and $nor
returns documents that fail all provided conditions.
$or
Find users whose age
is greater than 40 OR status
is "active":
db.users.find({
$or: [
{ age: { $gt: 40 } },
{ status: "active" }
]
});
$and
Find users older than 25 AND with status
"active":
db.users.find({
$and: [
{ age: { $gt: 25 } },
{ status: "active" }
]
});
$not
Find users where age
is not greater than 30:
db.users.find({ age: { $not: { $gt: 30 } } });
3. Array and Element Operators
In MongoDB, array and element operators are used to query documents that contain arrays or to check the existence and characteristics of fields within documents. These operators allow you to filter data based on array contents, array size, or the presence of specific elements. Commonly used ones include $in
(to match any value from a specified array), $all
(to ensure an array contains all specified elements), $size
(to match arrays of a specific length), and $elemMatch
(to match documents containing array elements that satisfy multiple conditions)
$in
Find users whose city
is either "Delhi" or "Mumbai":
db.users.find({ city: { $in: ["Delhi", "Mumbai"] } });
$nin
Find users not in a list of cities:
db.users.find({ city: { $nin: ["Delhi", "Mumbai"] } });
$exists
Find documents where email
field exists:
db.users.find({ email: { $exists: true } });
University: Sri Balaji University, Pune
School: School of Computer Studies
Course: BCA (Bachelor of Computer Applications)
Interests: NoSQL, MongoDB, and related technologies
📸 Instagram 🔗 LinkedIn 🌐 Official Website
nice explain
ReplyDeleteExcellent
ReplyDeleteExcellent
ReplyDeleteExcellent Work 👏
ReplyDeleteNice exaplanation
ReplyDeleteGreat work 👏
ReplyDeleteNice job 👍
ReplyDeleteFantastic job
ReplyDeleteBrilliant work
ReplyDeleteExcellent
ReplyDeleteGreat work 👍🏼
ReplyDeleteExcellent
ReplyDeleteGood information 👍
ReplyDeleteWell Explanation about Query Operators👍
ReplyDeleteNice explanation 👍
ReplyDeleteExcellent work and good information 👍
ReplyDeleteInformative and clear.
ReplyDeleteVery Nice explanation good job
ReplyDeleteGreat Explanation!
ReplyDeleteGreat work 👍🏻
ReplyDeleteGood explanation👍
ReplyDeleteGreat Job Om
ReplyDeleteSophisticated explanation, well done.
ReplyDeleteNice
ReplyDeleteGood work 👍🏻
ReplyDeleteGood Work
ReplyDeleteSo informative and well-researched. I appreciate the effort you put into this piece!
ReplyDeleteVERY WELL EXPLAINED
ReplyDeleteGood
ReplyDeleteNice
ReplyDeleteGreat work
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteExcellent 👍
DeleteExcellent Work Brothers 👍🔥♥️
ReplyDeleteExcellent work 👍 well done
ReplyDeleteVery Nice explained
ReplyDeleteNice explained
ReplyDeleteniece and very informative
ReplyDeleteVery insightful
ReplyDeleteNiceee 👍
ReplyDeleteNice Explanation
ReplyDelete