• Node.js Video Tutorials

Node.js - MongoDB Limit



The Limit() method in MongoDB has a similar effect as in Limit clause in SQL. It restricts the number of documents returned by find() query to a specified number. The limit() method takes a single integer as argument. It the argument is omitted, it implies that no limit has been applied.

To understand how the limit() function works, we shall use the orders collection in our database. Out of the available documents, those with price greater than or equal to 10 can be retrieved by the following code −

Example

const {MongoClient} = require('mongodb');

async function main(){

   const uri = "mongodb://localhost:27017/";
   const client = new MongoClient(uri);

   try {
      await client.connect();
      await limitdocs(client, "mydb", "orders");
   } finally {
      await client.close();
   }
}

main().catch(console.error);


async function limitdocs(client, dbname, colname){
   var myqry = {numPurchased:{$gte:10}};
   const result = await client.db(dbname).collection(colname).find({"numPurchased":{$gte:10}}).toArray();
   console.log(JSON.stringify(result));
}

Output

[{"_id":"658d7f3b30a92c8e5018a43a","orderId":201,"custid":301,"prodId":100,"numPurchased":20},{"_id":"658d7f3b30a92c8e5018a43b","orderId":202,"custid":302,"prodId":101,"numPurchased":10},{"_id":"658d7f3b30a92c8e5018a43d","orderId":204,"custid":303,"prodId":103,"numPurchased":15},{"_id":"658d7f3b30a92c8e5018a43e","orderId":205,"custid":303,"prodId":103,"numPurchased":20},{"_id":"658d7f3b30a92c8e5018a441","orderId":208,"custid":301,"prodId":100,"numPurchased":10},{"_id":"658d7f3b30a92c8e5018a442","orderId":209,"custid":303,"prodId":103,"numPurchased":30}] 

Now we will apply a limit to the result returned by find() query to 1. The syntax of limit() method is as follows −

collection.find(query).limit(number);

Change the limitdocs() function to the following code, to obtain number of documents limited to the specified number.

async function limitdocs(client, dbname, colname){
   var myqry = {numPurchased:{$gte:10}};
   const result = await client.db(dbname).collection(colname).find({"numPurchased":{$gte:10}}).limit(1).toArray();
   console.log(JSON.stringify(result));
}

Output

[{"_id":"658d7f3b30a92c8e5018a43a","orderId":201,"custid":301,"prodId":100,"numPurchased":20}]

The find() query fetches the documents tht atisfy the filter condition, starting from its first occurrence. If you want to skip certain number of documents, use the skip() clause.

collection.find(query).limit(x).skip(y)

Out of the documents returned by find(), only x documents will populate the resultset, out of which first y documents will be removed.

The following limitdocs() function return the second document in orders collection that has price>=10

async function limitdocs(client, dbname, colname){
   var myqry = {numPurchased:{$gte:10}};
   const result = await client.db(dbname).collection(colname).find({"numPurchased":{$gte:10}}).limit(1).skip(1).toArray();
   console.log(JSON.stringify(result));
}

Output

[{"_id":"658d7f3b30a92c8e5018a43b","orderId":202,"custid":302,"prodId":101,"numPurchased":10}]
Advertisements