w3resource

MongoDB Update Operators

Description

A conditional operator compares two expressions and fetched documents from mongodb collection. In this page we are going to discuss about the conditional operators and usage of conditional operators.

In MongoDB the conditional operators are :

(>) greater than - $gt

(<) less than - $lt

(>=) greater than equal to - $gte

(<= ) less than equal to - $lte

Our database name is 'myinfo' and our collection name is 'testtable'. Here, is the collection bellow.

Sample collection "testtable"

mongodb testtable in dot notation sample collection

MongoDB (>) greater than operator - $gt

If we want to fetch documents from the collection "testtable" which contains the value of "age " is more than 22, the following mongodb command can be used :

>db.testtable.find({age : {$gt : 22}})

Sql equivalent command is

Select * from testtable where age >22;

Output of the command

MongoDB greater than operator

MongoDB (>=) greater than equal to operator - $gte

If we want to fetch documents from the collection "testtable" which contains the value of "age " is more than or equla to 22, the following mongodb command can be used :

>db.testtable.find({age : {$gte : 22}})

Sql equivalent command is

Select * from testtable where age >=22;

Output of the command

MongoDB greater than equal to

MongoDB (<) less than operator - $lt

If we want to fetch documents from the collection "testtable" which contains the value of "age " is less than 19, the following mongodb command can be used :

>db.testtable.find({age : {$lt : 19}})

Sql equivalent command is

Select * from testtable where age <19;

Output of the command

MongoDB less than operator

MongoDB (<=) less than equal to operator - $lte

If we want to fetch documents from the collection "testtable" which contains the value of "age " is less than or equal to 19, the following mongodb command can be used :

>db.testtable.find({age : {$lte : 19}})

Sql equivalent command is

Select * from testtable where age <=19;

Output of the command

MongoDB less than equal to operator

MongoDB query using (<) and (>) operator - $lt and $gt

If we want to fetch documents from the collection "testtable" which contains the value of "age " is greater than 17 and less than 24, the following mongodb command can be used :

>db.testtable.find({age : {$lt :24, $gt : 17}})

Sql equivalent command is

Select * from testtable where age <24 and age>17;

Output of the command

MongoDB greater than and less than operator


Follow us on Facebook and Twitter for latest update.