Wednesday, January 29, 2014

Comparison operators in MongoDB

In this post we will explore the Comparison operators in MongoDB. Just like RDBMS such as Oracle, SQL Server, DB2 have their comparison operators, MongoDB has its own set of Comparison operators.

RDBMS Operator MongoDB Operator
= (Equal) :
< (Less than) $lt
<= (Less than or equal to) $lte
> (Greater than) $gt
>= (Greater than or equal to) $gte
!= (Not equal to) $ne

Let us see example of MongoDB operators.

Equal (:)

Here is an example of Equal(:) operator. We are reading data from deptdetails collection where DeptName is equal to Admin.

db.deptdetails.find({DeptName:"Admin"}).pretty()

Less than ($lt)

Here is example of less than($lt) operator. We are reading all the document (row) from deptdetails collection (table) where deptcode is less than 30.

db.deptdetails.find({DeptCode:{$lt:30}}).pretty()

Greater than ($gt)

Here is example of greater than($gt) operator. We are reading all the document (row) from deptdetails collection (table) where deptcode is greater than 30.

db.deptdetails.find({DeptCode:{$gt:30}}).pretty()

Not equal to ($ne)

Here is example of Not equal to ($ne) operator. We are reading all the document (row) from deptdetails collection (table) where deptcode is not equal to 30.

db.deptdetails.find({DeptCode:{$ne:30}}).pretty()

Similarly we can use less than or equal to and Greater than and equal to operator.

What is MongoDB?
How to get Started with MongoDB
How to create or drop Database in MongoDB?
How to create Collection (Table) in MongoDB?
... More

Popular Posts

Real Time Web Analytics