Introduction to MongoDB Query Operators

Find an employee whose salary is equal to 10000 from the collection

 
db.employee.find({salary:{$eq:10000}})

Find an employee whose address is vellore

 
db.employee.find({"address":{$eq:"vellore"}})

Finding an employee whose salary is greater than 100000

 
db.employee.find({"salary":{$gt:100000}})

Find an employee whose salary >= 300000

 
db.employee.find({"salary":{$gte:30000}})

Find all the employees from vellore and mumbai

 
db.employee.find({"address":{$in:["vellore","mumbai"]}})

Find all the employees whose salary less than 50000

 
db.employee.find({"salary":{$lt:50000}})

Find all the employees whose address not equal to delhi

 
db.employee.find({"address":{$ne:"delhi"}})

Find all the employees whose address is not in delhi or mumbai

 
db.employee.find({"address":{$nin:["delhi","mumbai"]}})

Logical operators

Display all the employees who earn a salary greater than 10000 and are from SCOPE

 
db.employee.find({$and:[{"salary":{$gt:10000}},{"school":{$eq:"scope"}}]})

Display all employees who are from SCOPE or whose salary is greater than 20000

 
db.employee.find({$or:[{"salary":{$gt:20000}},{"school":{$eq:"scope"}}]})

$nor operator Example

 
db.employee.find({$nor:[{"salary":{$gt:50000}},{"school":{$eq:"scope"}}]})

Display all employees who school is not equal to SCOPE

 
db.employee.find({school:{$not:{$eq:"scope"}}})

Display the employees whose document contains the salary field

 
db.employee.find({salary:{$exists:true}})

Display the employees whose name field is of String type

 
db.employee.find({name:{$type:"string"}})