MongoDB: Querying with Shell

In this post you can find tutorials on

  • Basic querying
  • Limiting results
  • Sorting results
  • Conditional expression
  • Regular Expression

Generating a dummy dataset for this post
Use command in shell.

for(var i=0;i<100000;i++){
	db.employee.insert({"empId": "emp_" + i ,"employeeName": "employee_" + i});

Basic querying

Function: find()
find is used to perform query fetch data from collections in MongoDB.



Returns all rows from collection. In shell it’ll returns only 20 rows and force user to get more by hitting “it” on shell.

Function: findOne()
findOne() is usually returns first matching document.



Restrict a search, i.e adding a criteria to query.

Function: find({…});
Here you can add condition to restrict your result, you submit criteria in KEY: VALUE pair.


Db.collectionName.find({“criteriakey”: “criteriaMatchingValue”});

For example:

Db.employee.find({“employeeName”: “employee_100”});

Returns :
Returns all rows having value “employee_100” in employeename field.

Results in :

{ "_id" : ObjectId("535971c1fe0e645d73ae8f52"), "empId" : "emp_100", 
"employeeName" : "employee_100" }

you can add multiple criteria also like db.colletionName.find({“key1”: “value1”, “key2”: “value2”, … , “keyn”: “valuen”});

Restrict columns from result

Here you can restrict no of columns from dataset.


Db.collectionName.find({“criteriakey”: “criteriaMatchingValue”}, {“projectionKey”: 1/0});

If you want particular column than give value as 1 to projectionKey, 0(zero) otherwise.

For example:
If you want to hide empId than

> db.employee.find({"employeeName": "employee_100"}, {"empId": 0});

Results in

{ "_id" : ObjectId("535971c1fe0e645d73ae8f52"), "employeeName" : "employee_100"

You can add multiple key also.

For example:

> db.employee.find({"employeeName": "employee_100"}, {"empId": 0, "_id": 0});

Results in:

{ "employeeName" : "employee_100" }

Limiting Results
Generally when you give a command to select than MongoDB default returns 20 rows in shell, but when you connected with any MongoDB driver it’ll returns all rows(that we’ll demonstrate in next post which is “MongoDB Querying with Java”). So you have to limiting a result.

Limiting a result

Function: limit(n);
Here n means no of rows you want from resultant dataset.



First n matching rows from pipe.

For example:


Results in:

> db.employee.find().limit(2);

{ "_id" : ObjectId("535971c1fe0e645d73ae8eee"), "empId" : "emp_0", 
"employeeName" : "employee_0" }
{ "_id" : ObjectId("535971c1fe0e645d73ae8eef"), "empId" : "emp_1", 
"employeeName" : "employee_1" }

Skipping a result

Function: skip(n);
Here n means no of rows you want from resultant dataset.



Returns all rows but skip first n rows from result.

For example:


Results in:

> db.employee.find().skip(2);

{"_id" : ObjectId("535971c1fe0e645d73ae8ef0"), "empId":"emp_2", "employeeName":"employee_2"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef1"), "empId":"emp_3", "employeeName":"employee_3"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef2"), "empId":"emp_4", "employeeName":"employee_4"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef3"), "empId":"emp_5", "employeeName":"employee_5"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef4"), "empId":"emp_6", "employeeName":"employee_6"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef5"), "empId":"emp_7", "employeeName":"employee_7"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef6"), "empId":"emp_8", "employeeName":"employee_8"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef7"), "empId":"emp_9", "employeeName":"employee_9"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef8"), "empId":"emp_10", "employeeName":"employee_10"}
{"_id" : ObjectId("535971c1fe0e645d73ae8ef9"), "empId":"emp_11", "employeeName":"employee_11"}
{"_id" : ObjectId("535971c1fe0e645d73ae8efa"), "empId":"emp_12", "employeeName":"employee_12"}
{"_id" : ObjectId("535971c1fe0e645d73ae8efb"), "empId":"emp_13", "employeeName":"employee_13"}
{"_id" : ObjectId("535971c1fe0e645d73ae8efc"), "empId":"emp_14", "employeeName":"employee_14"}
{"_id" : ObjectId("535971c1fe0e645d73ae8efd"), "empId":"emp_15", "employeeName":"employee_15"}
{"_id" : ObjectId("535971c1fe0e645d73ae8efe"), "empId":"emp_16", "employeeName":"employee_16"}
{"_id" : ObjectId("535971c1fe0e645d73ae8eff"), "empId":"emp_17", "employeeName":"employee_17"}
{"_id" : ObjectId("535971c1fe0e645d73ae8f00"), "empId":"emp_18", "employeeName":"employee_18"}
{"_id" : ObjectId("535971c1fe0e645d73ae8f01"), "empId":"emp_19", "employeeName":"employee_19"}
{ "_id" : ObjectId("535971c1fe0e645d73ae8f02"), "empId":"emp_20", "employeeName":"employee_20"}
{"_id" : ObjectId("535971c1fe0e645d73ae8f03"), "empId":"emp_21", "employeeName":"employee_21"}
Type "it" for more

Limiting + Skipping

Here we show use of limit and skip in combine.

If you want to skip 2 records and you also want only 2 records, than your query will be.

For example:


It’ll returns 2 object after skipping first 2 object.

Results in :

{ "_id" : ObjectId("535971c1fe0e645d73ae8ef0"), "empId" : "emp_2",
 "employeeName" : "employee_2" }
{ "_id" : ObjectId("535971c1fe0e645d73ae8ef1"), "empId" : "emp_3", 
"employeeName" : "employee_3" }

Sorting Results

You can sort results by fields while querying.


Db.CollectionName.find().sort({fieldname1: -1/1, ieldname2: -1/1, …,fieldnameN: -1/1});

Here -1 means Descending, and 1 means Ascending.

Returns objects after sorting in given order.

For example:

Db.employee.find().sort({empId: -1}).limit(2);

Results in:

{ "_id" : ObjectId("535971cefe0e645d73b0158d"), "empId" : "emp_99999", 
"employeeName" : "employee_99999" }
{ "_id" : ObjectId("535971cefe0e645d73b0158c"), "empId" : "emp_99998", 
"employeeName" : "employee_99998" }

Conditional Expression

Mongodb supports following comparison operator.

  • “$lt” – less than ( < )
  • “$gt” – greater than ( > )
  • “$lte” – less than or equal ( <= )
  • “$gte” – greater than or equal ( >= )
  • “$ne” – not equal( <> )

if we want to all employee whose age is less than 60 and greater than 50.

For example:

Db.employee.find({age: {"$gt": 50, "$lt": 60}});

All employee objects having age is greater than 50 and less than 60.

Use of “$ne”
Use of not equal operator is little bit different comparing to other operator.
If you want to all employee whose name is not “employee_4” than you query is.

For example:

Db.employee.find({“employeeName”: {“$ne”: “employee_4”}});

Skip object having employeeName = employee_4 , and returns others.

Regular Expression

Regular expressions are useful for flexible string searching.

Suppose you want to match string in case insensitive manner than use

db.employee.find({“employeeName”: /employee_4/i});

I means it’ll match string in case insensitive manner.

Another example of character matching is

Db.employee.find({“employeeName”: /employee_4?/});