MongoDB Tutorial

  • Home
  • Getting Started
  • CRUD
  • Indexes
Home / MongoDB CRUD / MongoDB limit

MongoDB limit

Summary: in this tutorial, you’ll learn how to use the MongoDB limit() method to specify the number of documents returned by a query.

Introduction to MongoDB limit() method

The find() method may return a lot of documents to the application. Typically, the application may not need that many documents.

To limit the number of returned documents, you use the limit() method:

db.collection.find(<query>).limit(<documentCount>)

The <documentCount> is in the range of -231 and 231. If you specify a value for the <documentCount> that is out of this range, the behavior of the limit() is unpredictable.

If the <documentCount> is negative, the limit() returns the same number of documents as if the <documentCount> is positive. In addition, it closes the cursor after returning a single batch of documents.

If the result set does not fit into a single batch, the number of returned documents will be less than the specified limit.

If the <documentCount> is zero, then is is equivalent to setting no limit.

Note that the limit() is analogous to the LIMIT clause in SQL.

To get the predictable result set using the limit(), you need to sort the result set first before applying the method like this:

cursor .sort({...}) .limit(<documentCount>)

In practice, you often use the limit() with the skip() method to paginate a collection.

The skip() method specifies from where the query should start returning the documents:

cursor.skip(<offset>)

The following shows the documents of the page pageNo with the documentCount documents per page:

db.collection.find({...} ).sort({...} ).skip(pageNo > 0 ? ( ( pageNo - 1 ) * documentCount) : 0 ).limit(documentCount);

The skip(<offset>) requires the MongoDB server to scan from the beginning of the result set before starting to return the documents. When the <offset> increases, the skip() will become slower.

Note that the limit() and skip() is analogous to the LIMIT OFFSET clause in SQL.

MongoDB limit() examples

We’ll use the following products collection:

db.products.insertMany([ { "_id" : 1, "name" : "xPhone", "price" : 799, "releaseDate" : ISODate("2011-05-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 6.5, "cpu" : 2.66 }, "color" : [ "white", "black" ], "storage" : [ 64, 128, 256 ] }, { "_id" : 2, "name" : "xTablet", "price" : 899, "releaseDate" : ISODate("2011-09-01T00:00:00Z"), "spec" : { "ram" : 16, "screen" : 9.5, "cpu" : 3.66 }, "color" : [ "white", "black", "purple" ], "storage" : [ 128, 256, 512 ] }, { "_id" : 3, "name" : "SmartTablet", "price" : 899, "releaseDate" : ISODate("2015-01-14T00:00:00Z"), "spec" : { "ram" : 12, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "blue" ], "storage" : [ 16, 64, 128 ] }, { "_id" : 4, "name" : "SmartPad", "price" : 699, "releaseDate" : ISODate("2020-05-14T00:00:00Z"), "spec" : { "ram" : 8, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256, 1024 ] }, { "_id" : 5, "name" : "SmartPhone", "price" : 599, "releaseDate" : ISODate("2022-09-14T00:00:00Z"), "spec" : { "ram" : 4, "screen" : 9.7, "cpu" : 1.66 }, "color" : [ "white", "orange", "gold", "gray" ], "storage" : [ 128, 256 ] }, { "_id" : 6, "name" : "xWidget", "spec" : { "ram" : 64, "screen" : 9.7, "cpu" : 3.66 }, "color" : [ "black" ], "storage" : [ 1024 ] }, { "_id" : 7, "name" : "xReader", "price" : null, "spec" : { "ram" : 64, "screen" : 6.7, "cpu" : 3.66 }, "color" : [ "black", "white" ], "storage" : [ 128 ] } ])

1) Using MongoDB limit() to get the most expensive product

The following example uses the limit() method to get the most expensive product in the products collection. It includes the _id, name, and price fields in the returned documents:

db.products.find({}, { name: 1, price: 1 }).sort({ price: -1 }).limit(1);

Output:

{ "_id" : 2, "name" : "xTablet", "price" : 899 }

In this example, we sort the products by prices in descending order and use limit() to select the first one.

The products collection has two products at the same price 899. The returned document depends on the order of documents stored on the disk.

To get the predictable result, the sort should be unique. For example:

db.products.find({}, { name: 1, price: 1 }).sort({ price: -1, name: 1 }).limit(1);

Output:

{ "_id" : 3, "name" : "SmartTablet", "price" : 899 }

This example sorts the document by prices in descending order. And then it sorts the sorted result set by names in ascending order. The limit() returns the first document in the final result set.

2) Using MongoDB limit() and skip() to get the paginated result

Suppose you want to divide the products collection into pages, each has 2 products.

The following query uses the skip() and limit() to get the documents of the second page:

db.products.find({}, { name: 1, price: 1 }).sort({ price: -1, name: 1 }).skip(2).limit(2);

Output:

{ "_id" : 1, "name" : "xPhone", "price" : 799 } { "_id" : 4, "name" : "SmartPad", "price" : 699 }

Summary

  • Use limit() to specify the number of returned documents for a query.
  • Use sort() and limit() to select top / bottom N documents.
  • Use skip() and limit() to paginate the documents in a collection.
  • Was this tutorial helpful ?
  • YesNo
Previous MongoDB sort
Next MongoDB updateOne

Getting Started

  • What is MongoDB
  • Install MongoDB
  • MongoDB Basics
  • MongoDB Shell
  • MongoDB Data Types

Inserting Documents

  • insertOne
  • insertMany
  • mongoimport

Selecting Documents

  • findOne
  • find
  • Projection: Selecting Returned Fields

Comparison Query Operators

  • $eq: Equal To Operator
  • $lt: Less Than Operator
  • $lte: Less Than or Equal To Operator
  • $gt: Greater Than Operator
  • $gte: Greater Than or Equal To Operator
  • $ne: Not Equal To Operator
  • $in: In Operator
  • $nin: Not In Operator

Logical Query Operators

  • $and: Logical AND Opeartor
  • $or: Logical OR Operator
  • $not: Logical NOT Operator
  • $nor: Logical NOR Operator

Element Query Operators

  • $exists
  • $type

Array Query Operators

  • $size
  • $all
  • $elemMatch

Sorting & Limiting

  • sort(): Sorting documents
  • limit(): Limiting documents

Updating Documents

  • updateOne: Update one Document
  • updateMany: Update Multiple Documents
  • $inc: Increase / Decrease Field Value
  • $min: Update Field Value
  • $max: Update Field Value
  • $mul: Mutiply Field By a Number
  • $unset: Remove Fields
  • $rename: Rename Fields
  • Upsert

Deleting Documents

  • deleteOne
  • deleteMany

About MongoDBTutorial.com

This MongoDB Tutorial helps you master MongoDB quickly.

Recent Tutorials

  • MongoDB Indexes
  • MongoDB limit
  • MongoDB sort
  • MongoDB Upsert
  • MongoDB Projection

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Copyright © 2021 · By mongodbtutorial.org. All Rights Reserved.