Quantcast
Channel: CodeFari
Viewing all articles
Browse latest Browse all 265

Fetch records using limit() and skip() method from MongoDB Collection

$
0
0
limit(): To fetch limited records from MongoDB collection we need to use limit() method. We need to pass number in limit() method as parameter, which number of documents that we want to display.
skip(): skip() method is responsible to skip number of documents, it accept number type argument.
The Limit() Method
The limit() Method is accepts one number type argument which is number of document that we want to display in MongoDB Collection data.
 SYNTAX: Below is given Basic syntax of limit() method is as follows


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


EXAMPLE: Consider the collection tesCol has the below given data


{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Codefari query overview"}



Below given example will display only 2 documents while querying the document.


>db.tesCol.find({},{"title":1,_id:0}).limit(2)
{"title":"MongoDB Overview"}
{"title":"NoSQL Overview"}
> 


when we don't specify number argument in limit() method then it will show all documents from the collection.

MongoDB Skip() Method
Apart from limit() method there is one more method skip() which also accepts number type argument and used to skip number of documents.
SYNTAX: Basic syntax of skip() method is as follows


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


EXAMPLE: Following example will only display only second document.


>db.tesCol.find({},{"title":1,_id:0}).limit(1).skip(1)
{"title":"NoSQL Overview"}
> 



Please note default value in skip() method is 0

Viewing all articles
Browse latest Browse all 265

Trending Articles