MongoDB Query Operators

Share via:
MongoDB Query Operators

There are different types of Operators available in MongoDB which are used to filter the data.
1 – Comparison operators
2 – Logical operators
3 – Element operators
4 – Evaluation operators
5 – Geospatial operators
6 – Array operators
7 – Bitwise operators
8 – Projection operators
9 – Miscellaneous operators
Comparison operators:
MongoDB comparison operators are used to compare the field values in one or multiple documents.
They are of eight comparison operators in MongoDB:
1. Equal to ($eq)
2. Not Equal to ($ne)
3. Greater than ($gt)
4. Less Than ($lt)
5. Greater Than or equal to ($gte)
6. Less than or equal to ($lte)
7. Matches any of the given value in the array ($in)
8. Matches no values in the array ($nin)
Common syntax for all comparison operators is given below
db.collection_name.find( { field_name:{operator_to_use : value } } )
1. Equal to ( $eq):
This operator is used to select the documents when the value of a given field equals the specified value.
Syntax:
db.collectionname.find({field_name :{ $eq: specific value }})
Example: In a collection named books get all the documents whose price is 1000
db.books.find({price :{ $eq:1000 }})
2. Not Equal to ($ne):
This operator is used to select the documents in a collection when the value of a given field does not equal the specified value.
This will include the document which does not have the mentioned field in the query
Syntax:
db.collectionname.find({field_name :{ $ne: specific value }})
Example: In a collection named books get all the documents whose price is not 100
db.books.find({price :{ $ne:100 }})
3. Greater than ($gt):
This operator is used to select the documents in a collection when the value of a given field is greater than the specified value.
Syntax:
db.collectionname.find({field_name :{ $gt: specific value }})
Example: In a collection named books get all the documents whose price is greater than 100
db.books.find({price :{ $gt:100 }})
4. Less than ($lt):
This operator is used to select the documents in a collection when the value of a given field is less than the specified value.
Syntax:
db.collectionname.find({field_name :{ $lt: specific value }})
Example : In a collection named books get all the documents whose price is less than 900
db.books.find({price :{ $lt:900 }})
5. Greater than or equal to ($gte):
This operator is used to select the documents in a collection when the value of a given field is greater than the specified value.
Syntax:
db.collectionname.find({field_name :{ $gte: specific value }})
Example: In a collection named books get all the documents whose price is greater than or equal to 500
db.books.find({price :{ $gte:500 }})
6. Less than or equal to ($gte):
This operator is used to select the documents in a collection when the value of a given field is less than the specified value.
Syntax:
db.collectionname.find({field_name :{ $gte: specific value }})
Example: In a collection named books get all the documents whose price is Less than or equal to 500
db.books.find({price :{ $lte:500 }})
7. In ($in):
This operator is used to select the documents in a collection when the value of a field present in the range of the specified array.
Syntax:
db.collectionname.find({price:{$in: [value1,value2,value n] }})
Example: In a collection named books get all the documents whose price is in range of 1000 to 1500
db.books.find({$in:{ [1000,1500] }}})
8. Not In ($nin):
This operator is used to select the documents in a collection when the value of a field, Not present in the range of the specified array.
Syntax:
db.collectionname.find({price:{$nin: [value1,value2,value n] }})
Example: In a collection named books get all the documents whose price is not in range of 160 to 180
db.books.find({$nin:{ [160,180] }}})
Logical operators
MongoDB logical operators are used to filter the data based on the multiple conditions.
They are of four types
Logical AND ($and)
Logical OR ($or)
Logical NOR ($nor)
Logical NOT ($not)
1. Logical AND ($and):
This operator will perform logical AND, on an array, having more than one expression, this selects the documents that match with all the expressions in the array of expressions.
Syntax:
db.collectionname.find({$and: [{expression1},{expression2},{expressionN}] })
Example: In a collection named books get all the documents whose price is 1500 and genre in french
db.books.find({$and:[ {price:{$eq:1500}}, {genre:”french”} ] })
2. Logical OR ($or):
This operator will perform logical OR on an array having more than one expression, this selects the documents that match with at least one of the expressions in the array of expressions.
Syntax:
db.collectionname.find({$or: [{expression1},{expression2},{expressionN}] })
Example: In a collection named books get all the documents whose price is 1500 or genre in french
db.books.find({$or:[ {price:{$eq:1500}}, {genre:”french”} ] })
3. Logical NOR ($nor):
This operator will perform logical NOR on an array having more than one expression ,this selects the documents that don’t match with all the expressions in the array of expressions.
Syntax:
db.collectionname.find({$nor: [{expression1},{expression2},{expressionN}] })
Example: In a collection named books get all the documents whose price is not 1500 and genre is not english
db.books.find({$nor:[ {price:{$eq:1500}}, {genre:”french”} ] })
4. Logical NOT($not):
This operator will perform logical NOT on a specific expression that selects the documents that do not match the specified expression.
Applied on a single expression. This command selects the documents which do not have the specified field that you mentioned.
Syntax:
db.collectionname.find({field :{$nor: {expression} } })
Example: In a collection named books get all the documents whose price is not 1500
db.books.find({$not: {price:{$eq:1500}} })

 

Author    : Teja
LinkedIn : https://www.linkedin.com/in/teja-sai-nadh-reddy-tatireddy-048882201
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTExperts is always active on social media platforms.
Facebook  : https://www.facebook.com/ktexperts/
LinkedIn    : https://www.linkedin.com/company/ktexperts/
Twitter       : https://twitter.com/ktexpertsadmin
YouTube   :  https://www.youtube.com/c/ktexperts
Instagram  : https://www.instagram.com/knowledgesharingplatform
Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment