MongoDB - SPLessons

MongoDB Limit

Chapter 25

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Limit

MongoDB Limit

shape Description

MongoDB Limit cursor is used for retrieving only specific documents or even all the documents that matching the given criteria. MongoDB Limit cursor takes a number and returns only that number of documents, if they exist, if less than the limit exists only those would be returned, and the cursor can be made efficient on the server because the server known it doesn’t need to prepare any more documents, it can a creates a much smaller data structures.

shape Examples

By viewing the below example, one can easily understand the concept of MongoDB Limit.

MongoDB shell version:2.6.1
connecting to: test
>use test
switched to db test
>show collections
names
system.indexes
>db.names.find();
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur = db.names.find();
>cur
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c3"), "name":"Dev D"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c4"), "name":"Emli"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c5"), "name":"Farshan"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c6"), "name":"Gangs"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c7"), "name":"Hum"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c8"), "name": 25}
>var cur =db.names.find();
>cur.limit(3);
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c0"), "name":"Alia"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c1"), "name":"Bedo"}
{ "_id": ObjectID("53be5d4604cc1cb0a7bfc3c2"), "name":"Chameli"}

Here in the above example the limit() will limit the collections in mongoDB program. And the above program has assigned a limit permission to number(3), so it will give the output values of first three collections.

Summary

shape Key Points

  • MongoDB Limit cursors – It will retrieves the document that is matching the given criteria.