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.

Basic querying

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

Syntax:

Returns:
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.

Syntax:

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.

Syntax:

For example:

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

Results in :

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.

Syntax:

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

For example:
If you want to hide empId than

Results in

You can add multiple key also.

For example:

Results in:

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.

Syntax:

Returns:
First n matching rows from pipe.

For example:

Results in:

Skipping a result

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

Syntax:

Returns:
Returns all rows but skip first n rows from result.

For example:

Results in:

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:

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

Results in :

Sorting Results

You can sort results by fields while querying.

Syntax:

Here -1 means Descending, and 1 means Ascending.

Returns:
Returns objects after sorting in given order.

For example:

Results in:

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:

Returns:
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:

Returns:
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

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

Another example of character matching is