Limit, Skip, Sort and count in MongoDB

This article will cover:

Limit in MongoDB

In order to limit the number of returned result in find method, you need to use the "Limit" method. The limit method takes one argument of type number, which is the number of document you want to retrieve from a specific collection.

Preparing data

Imagine that our collection contains the following documents

{ "_id" : ObjectId("5e35a076890d645061ea7db8"), "name" : "article 1" }
{ "_id" : ObjectId("5e35a076890d645061ea7db9"), "name" : "article 2" }
{ "_id" : ObjectId("5e35a076890d645061ea7dba"), "name" : "article 3" }
{ "_id" : ObjectId("5e35a076890d645061ea7dbb"), "name" : "article 4" }

Limiting the number of displayed documents

db.articles.find().limit(2);

Result

{ "_id" : ObjectId("5e35a076890d645061ea7db8"), "name" : "article 1" }
{ "_id" : ObjectId("5e35a076890d645061ea7db9"), "name" : "article 2" }

RDBMS equivalent of limit

select * from articles limit 2;

Skip in MongoDB

We use Skip method which takes an argument of type number to retrieve only the elements starting from the specified number.

Skip usage

db.articles.find().skip(2);

Result

{ "_id" : ObjectId("5e35a076890d645061ea7dba"), "name" : "article 3" }
{ "_id" : ObjectId("5e35bc7d890d645061ea7dbb"), "name" : "article 4" }

The combination of limit and skip is what let us create what we call a pagination system in MongoDB.

Pagination in MongoDB

Consider a case where you want to retrieve the second page where every page contains 2 documents. So in this case we should skip 2 multiply by 1 ("number of documents per page" * "number of page we want to skip") documents and get 2 documents starting from that point (See more: [Pagination in MongoDB using limit and skip]).

Pagination in MongoDB

db.articles.find().skip(2).limit(2);

Result

{ "_id" : ObjectId("5e35a076890d645061ea7dba"), "name" : "article 3" }
{ "_id" : ObjectId("5e35bc7d890d645061ea7dbb"), "name" : "article 4" }

RDBMS equivalent of limit

SELECT * FROM articles LIMIT 2 OFFSET 2

Sort in MongoDB

The sort method is used to sort the result of a query in ascending or descending order. The sort method takes an object that takes as keys the name of the fields we want to sort the result with and as values either 1 (descending) or -1 for descending (See more: [Sorting documents using sort method or $sort aggregation pipeline in MongoDB]).

Ascending Sort in MongoDB

db.articles.find().sort({name: 1});

RDBMS equivalent of ascending sort

SELECT * FROM articles order by name asc;

Descending Sort in MongoDB

db.articles.find().sort({name: -1});

RDBMS equivalent of descending sort

SELECT * FROM articles order by name desc;

Sort using multiple fields in MongoDB

db.articles.find().sort({name: -1, nbr: 1});

RDBMS equivalent of sort using multiple fields

SELECT * FROM articles order by name desc, nbr asc;

Count in MongoDB

The count method is used to count the number of documents. The count method takes one parameter which is a query (have the same syntax as the query parameter in find) (See more: [Counting the number of documents using count or aggregation framework in MongoDB]).

Counting the number of elements inside a MongoDB collection

db.articles.count({});

Result

4

RDBMS equivalent of ascending sort

SELECT count(*) FROM articles;

Counting the number of elements inside a MongoDB collection using a query parameter

db.articles.count({name: {$ne: "article 1"}});

Result

3

RDBMS equivalent of ascending sort

SELECT count(*) FROM articles where name!='article 1';