Contents of page >
GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions - using $where, $gt and $gte operator in MongoDB
First, Let's create new collection and insert document in it >
> db.employee.insert({_id : 1, firstName:"ankit"})
> db.employee.insert({_id : 2, firstName:"ankit", salary : 1000 })
> db.employee.insert({_id : 3, firstName:"sam", salary : 2000 })
> db.employee.insert({_id : 4, firstName:"neh", salary : 3000 })
|
First line above will create table (or collection) (if table already exists it will insert documents in it).
GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions.
1) GREATER THAN (>) - using $gt operator in MongoDB in MongoDB .
Find employee where
Salary > 1000
by using find method and $gt operator.
> db.employee.find( { salary : {$gt : 1000} } )
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
2) GREATER THAN EQUALS (>=) - using $gte operator.
Find employee where
Salary >= 1000
by using find method and $gt operator.
> db.employee.find( { salary : {$gte : 1000} } )
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
3) GREATER THAN (>) - using $where operator.
Find employee where
Salary > 1000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary > 1000"})
|
Output>
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
4) GREATER THAN EQUALS (>=) - using $where operator.
Find employee where
Salary >= 1000
by using find method and $where operator.
> db.employee.find({ $where:"this.salary >= 1000"})
|
Output>
{ "_id" : 2, "firstName" : "ankit", "salary" : 1000 }
{ "_id" : 3, "firstName" : "sam", "salary" : 2000 }
{ "_id" : 4, "firstName" : "neh", "salary" : 3000 }
|
5) SUMMARY>
So in this mongoDB tutorial we learned how to use GREATER THAN (>) and GREATER THAN EQUALS (>=) conditions - using $where, $gt and $gte operator in MongoDB.
GREATER THAN (>) - using $gt operator.
Find employee where
Salary > 1000
> db.employee.find( { salary : {$gt : 1000} } )
|
GREATER THAN EQUALS (>=) - using $gte operator.
Find employee where
Salary >= 1000
> db.employee.find( { salary : {$gte : 1000} } )
|
GREATER THAN (>) - using $where operator.
Find employee where
Salary > 1000
> db.employee.find({ $where:"this.salary > 1000"})
|
GREATER THAN EQUALS (>=) - using $where operator.
Find employee where
Salary >= 1000
> db.employee.find({ $where:"this.salary >= 1000"})
|
Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>