select(find) query with and/or operator condition in MongoDB

select(find) query with and/or operator condition in MongoDB

select * from employees

> 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
————-
select * from employees where status= ‘A’ and age > 30

db.employees.find(  { status: “A”,age:{$gt:30} })

> db.employees.find(  { status: “A”,age:{$gt:30} },{_id:0} )
{ “name” : “abc123”, “age” : 55, “status” : “A” }
{ “name” : “asd”, “age” : 35, “status” : “A” }

OR Operator
———–
select * FROM employees WHERE status = ‘A’ OR age = 50

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” }
>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.