Find Modifiers

find() modifiers are methods that modify behaviour of the cursor returned by the find() method. This chapter explains three main find modifiers, namely limit, skip and sort.

limit()

By default mongo shell takes the first 20 documents from the cursor of the find() method and prints it. The cursor can be iterated by typing "it" which gets the next 20 documents.

The limit() method sets the number of documents that a cursor should return. The syntax of limit() is as below

db.CollectionName.find().limit( n )

where n specifies the maximum number of documents the cursor can return.

skip()

The skip() method skips documents in a cursor there by controlling the starting point of the result set. The syntax of skip() is as below:

db.CollectionName.find().skip( n )

where n specifies the number of documents to be skipped.

sort()

The sort() methods sorts the documents in a cursor. The documents can be sorted in ascending or descending order on any field. The sort method takes a JSON style document as parameter to define the sort order on a field. The syntax is of sort() is as below:

db.CollectionName.find().sort( { field1: value, field2:value, ... } )

where value can be either -1 for descending or 1 for ascending order.

Find Modifier examples

Before you start these examples, create a test collection that contains many documents. This can be done easily by running a JavaScript program as shown below on the mongo shell.

> for ( i = 1; i <= 100;  i++ ) {
    db.test.insert(
    {
    idx: i,
    desc: "This is " + i
    }
    );
    }
>

The above program creates a collection named test that contains 100 documents

> db.test.count()
100
>

The following example uses limit() method to print the first 5 documents.

> db.test.find({},{_id:0}).limit(5)
{ "idx" : 1, "desc" : "This is 1" }
{ "idx" : 2, "desc" : "This is 2" }
{ "idx" : 3, "desc" : "This is 3" }
{ "idx" : 4, "desc" : "This is 4" }
{ "idx" : 5, "desc" : "This is 5" }
>

In the next example the skip() method is used to skip the first 5 documents and print the next 20

> db.test.find({},{_id:0}).skip(5)
{ "idx" : 6, "desc" : "This is 6" }
{ "idx" : 7, "desc" : "This is 7" }
{ "idx" : 8, "desc" : "This is 8" }
{ "idx" : 9, "desc" : "This is 9" }
{ "idx" : 10, "desc" : "This is 10" }
{ "idx" : 11, "desc" : "This is 11" }
{ "idx" : 12, "desc" : "This is 12" }
{ "idx" : 13, "desc" : "This is 13" }
{ "idx" : 14, "desc" : "This is 14" }
{ "idx" : 15, "desc" : "This is 15" }
{ "idx" : 16, "desc" : "This is 16" }
{ "idx" : 17, "desc" : "This is 17" }
{ "idx" : 18, "desc" : "This is 18" }
{ "idx" : 19, "desc" : "This is 19" }
{ "idx" : 20, "desc" : "This is 20" }
{ "idx" : 21, "desc" : "This is 21" }
{ "idx" : 22, "desc" : "This is 22" }
{ "idx" : 23, "desc" : "This is 23" }
{ "idx" : 24, "desc" : "This is 24" }
{ "idx" : 25, "desc" : "This is 25" }
Type "it" for more
>

In the example below the sort() method is used to sort the documents on the field idx in descending order

> db.test.find( { }, {_id: 0} ).sort( { idx: -1 } )
{ "idx" : 100, "desc" : "This is 100" }
{ "idx" : 99, "desc" : "This is 99" }
{ "idx" : 98, "desc" : "This is 98" }
{ "idx" : 97, "desc" : "This is 97" }
{ "idx" : 96, "desc" : "This is 96" }
{ "idx" : 95, "desc" : "This is 95" }
{ "idx" : 94, "desc" : "This is 94" }
{ "idx" : 93, "desc" : "This is 93" }
{ "idx" : 92, "desc" : "This is 92" }
{ "idx" : 91, "desc" : "This is 91" }
{ "idx" : 90, "desc" : "This is 90" }
{ "idx" : 89, "desc" : "This is 89" }
{ "idx" : 88, "desc" : "This is 88" }
{ "idx" : 87, "desc" : "This is 87" }
{ "idx" : 86, "desc" : "This is 86" }
{ "idx" : 85, "desc" : "This is 85" }
{ "idx" : 84, "desc" : "This is 84" }
{ "idx" : 83, "desc" : "This is 83" }
{ "idx" : 82, "desc" : "This is 82" }
{ "idx" : 81, "desc" : "This is 81" }
Type "it" for more
>

The modifiers can also be chained one after other.

In the following example the documents in the test collection are sorted in descending order on idx field, then the first 25 documents are skipped and the next 5 documents that follow are printed.

> db.test.find( {}, {_id:0 }).sort({ idx: -1 }).skip(25).limit(5)
{ "idx" : 75, "desc" : "This is 75" }
{ "idx" : 74, "desc" : "This is 74" }
{ "idx" : 73, "desc" : "This is 73" }
{ "idx" : 72, "desc" : "This is 72" }
{ "idx" : 71, "desc" : "This is 71" }
>

Last Updated: 03-July-2014


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

Nothing yet..be the first to share wisdom.