Use of where clause condition in MongoDB

Use of where condition in MongoDB

Example of using select(find) query with where condition in MongoDB.

Check the complete table detail as follows

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

Use of where clause with EQUAL operator in MongoDB

---In RDBMS example
select * from employees where status = 'A';

--In MongoDB
db.employees.find( {status: "A"} )

db.employees.find( {status:"A"},{_id:0})
Output:
{ "name" : "abc123", "age" : 55, "status" : "A" }
{ "name" : "qwe", "age" : 20, "status" : "A" }
{ "name" : "zxc", "age" : 25, "status" : "A" }
{ "name" : "asd", "age" : 35, "status" : "A" }

Use of IN-EQUAL operator in Where Clause

-- In RDBMS
select * from employees where status != 'A';

--In MongoDB
db.employees.find( { status: { $ne: "A" } } )

db.employees.find( { status: { $ne: "A" } }, {_id:0})
Output:
{ "name" : "kkk", "age" : 23, "status" : "N" }

Use of greater then operator in where clause

-- In RDBMS
SELECT * FROM employees WHERE age >= 25

--In MongoDB
db.employees.find(  { status: "A",age:{$gte:25} } )

db.employees.find(  { status: "A",age:{$gte:35} }, {_id:0})
OUTPUT:
{ "name" : "abc123", "age" : 55, "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 )

Connecting to %s

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