The usage of Limit and Skip in MongoDB is explained in detail

  • 2020-06-19 11:59:55
  • OfStack

The usage of Limit and Skip in MongoDB is explained in detail

1 MongoDB Limit() method

If you need to read a specified number of data records from MongoDB, use the Limit method of MongoDB. The limit() method takes a numeric argument that specifies the number of records read from MongoDB.

grammar

The basic syntax for the limit() method is as follows:


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

The instance


> db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP  The tutorial " }
{ "title" : "Java  The tutorial " }
>

Note: If you do not specify parameters in the limit() method, all data in the collection is displayed.

2 MongoDB Skip() method

In addition to using the limit() method to read a specified amount of data, you can also use the skip() method to skip a specified amount of data, and the skip method also accepts a numeric parameter as the number of records to skip.

grammar

The skip() method script syntax format is as follows:


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

The instance

The above instance displays only the second document data


>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java  The tutorial " }

The above is the introduction of the usage of Limit and Skip in MongoDB. If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading.


Related articles: