Limit records in MongoDB

Limit () method in MongoDB

To limit the records in MongoDB, you need to use the limit () method. The limit () method takes a parameter in a numeric format, which is the Document number you want to display.

Syntax

The basic limit () method syntax is as follows:

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

For example

You follow Collection with the name mycol with the following data:

 { "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } 

The following example shows only 2 Documents while querying.

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

If you do not specify the parameter in the limit () method, it will display all Documents from that Collection.

Skip () method in MongoDB

In addition to the limit () method, there is another method in MongoDB: skip () also takes a parameter in the numeric form and is used to jump over the specified Document number.

Syntax

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

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

For example

The following example will only display the second Document.

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

Remember that the default value in skip () is 0.

According to Tutorialspoint

Previous article: Projection in MongoDB

Next lesson: Arrange records in MongoDB

4 ★ | 1 Vote

May be interested

  • Query Document in MongoDBQuery Document in MongoDB
    to query data from collection in mongodb, you need to use the find () method in mongodb.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • ObjectId in MongoDBObjectId in MongoDB
    you have seen the use of mongodb objectid in previous chapters. in this chapter, we will understand the structure of objectid.
  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • HAVING clause in SQLHAVING clause in SQL
    the having clause in sql is used to filter records and retrieve only those records that match the requirements or are actually needed.
  • Learn about Java Driver in MongoDBLearn about Java Driver in MongoDB
    in the following article, we will introduce you some basic features of mongodv java driver as well as how to deploy and apply in practice.
  • SELECT TOP command in SQL ServerSELECT TOP command in SQL Server
    in sql server, the select top command is used to retrieve records from one or more tables in sql server and limit the number of return records based on a fixed value or percentage.
  • Install MongoDBInstall MongoDB
    instructions for installing mongodb on windows.
  • Map Reduce in MongoDBMap Reduce in MongoDB
    in mongodb documentation, map-reduce is a data processing system that condenses a large amount of data into useful overall results. mongodb uses mapreduce command for map-reduce operation. in general, map reduce is used to handle large data sets.
  • Data modeling in MongoDBData modeling in MongoDB
    data in mongodb has a flexible schema. documents in the same collection need not have the same set of fields or structures, and common fields in collection documents can keep different data types.