Use of AND / OR operator in MangoDB database
Example of Select(find) query with and/or operator condition in MongoDB
Check complete data present in Employees collection/table.
-- In RDBMS
select * from employees;
-- In MongoDB
db.employees.find( {},{_id:0})
{ "name" : "abc123", "age" : 55, "status" : "A" }
{ "name" : "qwe", "age" : 20, "status" : "A" }
{ "name" : "zxc", "age" : 25, "status" : "A" }
{ "name" : "asd", "age" : 35, "status" : "A" }
{ "name" : "kkk", "age" : 23, "status" : "N" }
And operator
Example show the use of AND operator in both MongoDB and RDBMS environment.
-- In RDBMS
select * from employees where status= 'A' and age > 30
-- In MongoDB
db.employees.find( { status: "A",age:{>:30} })
db.employees.find( { status: "A",age:{>:30} },{_id:0} )
{ "name" : "abc123", "age" : 55, "status" : "A" }
{ "name" : "asd", "age" : 35, "status" : "A" }
OR Operator
Example show the use of OR operator in both MongoDB and RDBMS environment.
-- In RDBMS
select * FROM employees WHERE status = 'A' OR age = 50
--In MongoDB
db.employees.find( { $or: [ { status: "A" } , { age: 50 } ] } )
db.employees.find( { $or: [ { status: "A" } , { age: 50 } ] },{_id:0} )
{ "name" : "abc123", "age" : 55, "status" : "A" }
{ "name" : "qwe", "age" : 20, "status" : "A" }
{ "name" : "zxc", "age" : 25, "status" : "A" }
{ "name" : "asd", "age" : 35, "status" : "A" }