TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MongoDB limit()

MongoDB limit() mehtod for beginners and professionals with examples on CRUD, insert document, query document, update document, delete document, use database, projection etc.

<< Back to MONGODB

MongoDB limit() Method

In MongoDB, limit() method is used to limit the fields of document that you want to show. Sometimes, you have a lot of fields in collection of your database and have to retrieve only 1 or 2. In such case, limit() method is used.

The MongoDB limit() method is used with find() method.

Syntax:

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

Scenario:

Consider an example which has a collection name TheDeveloperBlog.

This collection has following fields within it.

    [
      {
        Course: "Java",
        details: { Duration: "6 months", Trainer: "Sonoo Jaiswal" },
        Batch: [ { size: "Medium", qty: 25 } ],
        category: "Programming Language"
      },
      {
        Course: ".Net",
        details: { Duration: "6 months", Trainer: "Prashant Verma" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Medium", qty: 10 }, ],
        category: "Programming Language"
      },
      {
        Course: "Web Designing",
        details: { Duration: "3 months", Trainer: "Rashmi Desai" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Large", qty: 10 } ],
        category: "Programming Language"
      }
    ];

Here, you have to display only one field by using limit() method.

Example

db.TheDeveloperBlog.find().limit(1) 

After the execution, you will get the following result

Output:

{ "_id" : ObjectId("564dbced8e2c097d15fbb601"), "Course" : "Java", "details" : {
 "Duration" : "6 months", "Trainer" : "Sonoo Jaiswal" }, "Batch" : [ { "size" :
"Medium", "qty" : 25 } ], "category" : "Programming Language" } 

MongoDB skip() method

In MongoDB, skip() method is used to skip the document. It is used with find() and limit() methods.

Syntax

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

Scenario:

Consider here also the above discussed example. The collection TheDeveloperBlog has three documents.

    [
      {
        Course: "Java",
        details: { Duration: "6 months", Trainer: "Sonoo Jaiswal" },
        Batch: [ { size: "Medium", qty: 25 } ],
        category: "Programming Language"
      },
      {
        Course: ".Net",
        details: { Duration: "6 months", Trainer: "Prashant Verma" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Medium", qty: 10 }, ],
        category: "Programming Language"
      },
      {
        Course: "Web Designing",
        details: { Duration: "3 months", Trainer: "Rashmi Desai" },
        Batch: [ { size: "Small", qty: 5 }, { size: "Large", qty: 10 } ],
        category: "Programming Language"
      }
    ];

Execute the following query to retrieve only one document and skip 2 documents.

Example

db.TheDeveloperBlog.find().limit(1).skip(2)

After the execution, you will get the following result

Output:

{ "_id" : ObjectId("564dbced8e2c097d15fbb603"), "Course" : "Web Designing", "det
ails" : { "Duration" : "3 months", "Trainer" : "Rashmi Desai" }, "Batch" : [ { "
size" : "Small", "qty" : 5 }, { "size" : "Large", "qty" : 10 } ], "category" : "
Programming Language" }

As you can see, the skip() method has skipped first and second documents and shows only third document.


Next TopicMongoDB sort()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf