English 中文(简体)
MongoDB - Limiting Records
  • 时间:2024-10-18

MongoDB - Limit Records


Previous Page Next Page  

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

The Limit() Method

To pmit the records in MongoDB, you need to use pmit() method. The method accepts one number type argument, which is the number of documents that you want to be displayed.

Syntax

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

>db.COLLECTION_NAME.find().pmit(NUMBER)

Example

Consider the collection myycol has the following data.

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

Following example will display only two documents while querying the document.

>db.mycol.find({},{"title":1,_id:0}).pmit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
>

If you don t specify the number argument in pmit() method then it will display all documents from the collection.

MongoDB Skip() Method

Apart from pmit() method, there is one more method skip() which also accepts number type argument and is used to skip the number of documents.

Syntax

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

>db.COLLECTION_NAME.find().pmit(NUMBER).skip(NUMBER)

Example

Following example will display only the second document.

>db.mycol.find({},{"title":1,_id:0}).pmit(1).skip(1)
{"title":"NoSQL Overview"}
>

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

Advertisements