In this post we will explore the Logical operators of MongoDB. Just like RDBMS such as Oracle, SQL Server, DB2 have logical operators, MongoDB has its own set of Comparison operators.
RDBMS Logical Operators | MongoDB Operator |
AND | { |
OR | $or:[ ... ] |
Let us see example of MongoDB operators.
AND operator
In MongoDB the key/vaue pair passed into a curley braces ({...}) and separated by comma (,) act as AND logical operator. For example to read document (Row) from deptdetails (table) collection using AND operator we can write following command.
db.deptdetails.find({DeptCode:30,DeptName:"Accounts"}).pretty()
Here we are reading data where DeptName is Accounts AND DeptCode is 30.
OR operator
MongoDB has provided $or for OR operator. They key/value pair inside $or[ … ] is use to pass separate expression values. For example to read data from deptdeails collection (table) where DeptName is HR or DeptCode is 30 we can write the statement like this
db.deptdetails.find(
{
$or:
[
{DeptCode:30},
{DeptName:"HR"}
]
}
).pretty()
What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More