Tag Archives: where clause use

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