In previous two posts of MongoDB, i discussed about
1. What is MongoDB and how to install it
In this post i am going to discuss about MongoDB operators.
I finished last post by updating a new field in MongoDB called salary
1 |
db.emp.find(); |
1 2 3 4 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e1"), "designation" : "SE", "dob" : "09/07/1989", "firstname" : "raj", "gender" : "M", "lastname" : "kumar", "nationality" : "India", "salary" : 18000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e2"), "designation" : "Manager", "dob" : "10/08/1984", "firstname" : "robin", "gender" : "M", "lastname" : "yadav", "nationality" : "India", "salary" : 45000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e3"), "designation" : "TL", "dob" : "12/04/1986", "firstname" : "tang", "gender" : "M", "lastname" : "sharma", "nationality" : "India", "salary" : 20000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "designation" : "Employee", "dob" : "13/08/1983", "firstname" : "parul", "gender" : "F", "lastname" : "iti", "nationality" : "India", "salary" : 22000 } |
MongoDB Operators
$gt greater than.
$gte greater than or equal to.
$in matches value that exist in an array.
$lt less than.
$lte less than or equal to.
$ne not equal to.
$nin matches value that do not exist in an array.
Find the records of an employee whose salary is greater than 20000
In MySQL, we write
1 |
select * from emp where salary > 20000 |
1 |
db.emp.find({salary:{<strong>$gt</strong>:20000}}); |
1 2 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e2"), "designation" : "Manager", "dob" : "10/08/1984", "firstname" : "robin", "gender" : "M", "lastname" : "yadav", "nationality" : "India", "salary" : 45000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "designation" : "Emp", "dob" : "13/08/1983", "firstname" : "parul", "gender" : "F", "lastname" : "iti", "nationality" : "India", "salary" : 22000 } |
To only retrieve salary field
1 |
db.emp.find({salary:{<b>$gt</b>:20000}},{salary:1}); |
1 2 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e2"), "salary" : 45000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "salary" : 22000 } |
Salary is less than 20000
1 |
db.emp.find({salary:{<b>$lt</b>:20000}}); |
1 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e1"), "designation" : "SE", "dob" : "09/07/1989", "firstname" : "raj", "gender" : "M", "lastname" : "kumar", "nationality" : "India", "salary" : 18000 } |
Less than 20000 and greater than 40000
1 |
db.emp.find({salary:{<b>$gt</b>:20000,<b>$lt</b>:40000}}); |
1 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "designation" : "Emp", "dob" : "13/08/1983", "firstname" : "parul", "gender" : "F", "lastname" : "iti", "nationality" : "India", "salary" : 22000 } |
Find all the employees whose firstname is raj or robin
1 |
db.emp.find({firstname:{<b>$in</b>: ['raj','robin']}}); |
1 2 3 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e1"), "designation" : "SE", "dob" : "09/07/1989", "firstname" : "raj", "gender" : "M", "lastname" : "kumar", "nationality" : "India", "salary" : 18000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e2"), "designation" : "Manager", "dob" : "10/08/1984", "firstname" : "robin", "gender" : "M", "lastname" : "yadav", "nationality" : "India", "salary" : 45000 } |
Find all the employees whose firstname is not raj or robin
1 |
db.emp.find({firstname:{<b>$nin</b>: ['raj','robin']}}); |
1 2 3 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e3"), "designation" : "TL", "dob" : "12/04/1986", "firstname" : "tang", "gender" : "M", "lastname" : "sharma", "nationality" : "India", "salary" : 20000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "designation" : "Emp", "dob" : "13/08/1983", "firstname" : "parul", "gender" : "F", "lastname" : "iti", "nationality" : "India", "salary" : 22000 } |
Returns all rows whose salary is not equal to 45000
1 |
db.emp.find({salary:{<b>$ne</b>:45000}}); |
1 2 3 |
{ "_id" : ObjectId("529e9ecadd5194e25eac38e1"), "designation" : "SE", "dob" : "09/07/1989", "firstname" : "raj", "gender" : "M", "lastname" : "kumar", "nationality" : "India", "salary" : 18000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e3"), "designation" : "TL", "dob" : "12/04/1986", "firstname" : "tang", "gender" : "M", "lastname" : "sharma", "nationality" : "India", "salary" : 20000 } { "_id" : ObjectId("529e9ecadd5194e25eac38e4"), "designation" : "Emp", "dob" : "13/08/1983", "firstname" : "parul", "gender" : "F", "lastname" : "iti", "nationality" : "India", "salary" : 22000 } |
You can explore some more operators from MongoDB documentation.