MongoDB Limit number of records

Source: Internet
Author: User
Tags mongodb limit

MongoDB Limit () method

To limit the number of records returned in MongoDB, you need to use the limit () method. The method accepts a numeric type parameter, which is the number of documents to display.

Grammar

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

> DB. Collection_name.find (). Limit (number)
Shell

Example

Assume that the collection Myycol has the following data.

> db.mycol.find ({},{' _id ': 1, ' title ': 1})
{"_id": 101, "title": "MongoDB Guide"}
{"_id": 102, "title": "NoSQL Database"}
{"_id": 104, "title": "Python Quick Guide"}
{"_id": +, "title": "MongoDB Overview"}


The following example displays only two documents when querying a document.

> db.mycol.find ({},{"title": 1,_id:0}). Limit (2)
{"title": "MongoDB Guide"}
{"title": "NoSQL Database"}


If you do not specify the value of the number parameter in the limit () method, it displays all the documents in the collection.


MongoDB Skip () method

In addition to the limit () method, a method of Skip () also accepts a numeric type parameter, which is used to skip the number of documents.

Grammar

The basic syntax for the Skip () method is as follows-

>db. Collection_name.find (). Limit (number). Skip (number)


Example

The following example displays only a third document.

> db.mycol.find ({},{"title": 1,_id:0}). Limit (1). Skip (2)
{"title": "Python Quick Guide"}


Note that the default value in the Skip () method is 0.


MongoDB Limit number of records

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.