MongoDB – Limit Records

In this chapter, we will learn how to limit records using MongoDB.

The Limit() Method

You need to use the limit() method to limit records in MongoDB. The method accepts one argument for the number type, which is the number of documents that you want to show.


The basic syntax of limit() method is as follows −



Consider that myycol collection has the following details.

{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"},
{_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"},
{_id : ObjectId("507f191e810c19729de860e3"), title: "Codelivly Overview"}

The following example displays only two documents when the document is being querying.

{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}

If you do not specify the number argument in the limit() process, then all documents from the collection will be displayed.

MongoDB Skip() Method

In addition to the limit() method, there is one more skip() method that also accepts the argument for number type and is used to skip the number of documents.


The basic syntax of skip() method is as follows −



Following example will display only the second document.

{"title":"NoSQL Overview"}

Please note, the default value in skip() method is 0.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *