limit(): To fetch limited records from the MongoDB collection, we need to use the limit() method. We need to pass the number in the limit() method as a parameter,
which the number of documents that we want to display.
skip(): skip() method is responsible for skipping the number of documents, it accepts a number type
argument.
The Limit() Method
The limit() Method is accepted one number type argument which
is the number of the document that we want to display in MongoDB Collection data.
SYNTAX: The basic syntax of the limit() method is as follows.
>db.COLLECTION_NAME.find().limit(NUMBER)
|
EXAMPLE: Consider the collection tecsol contains the data below
{ "_id" : ObjectId(5983548781331adf45ec5),
"title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6),
"title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7),
"title":"Codefari query overview"}
|
The below 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 the limit() method, then it will show all
documents from the collection.
MongoDB Skip() Method
Apart from the limit() method, there is one more method skip() which also accepts number type
argument and used to skip the number of documents.
SYNTAX: Basic
syntax of skip() method is as
follows
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
|
EXAMPLE: The following example will display the 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
No comments:
Post a Comment
Please do not enter any spam link in the comment box.