- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
In MongoDB will limit() increase query speed?
No, using LIMIT() decreases the bandwidth consumption and it does not increase query speed. Let us see an example and create a collection with documents −
> db.demo197.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afde803d395bdc21346d8") } > db.demo197.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afdef03d395bdc21346d9") } > db.demo197.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afdf203d395bdc21346da") } > db.demo197.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afdf603d395bdc21346db") } > db.demo197.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afdf903d395bdc21346dc") } > db.demo197.insertOne({"Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afe1603d395bdc21346dd") } > db.demo197.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3afe2003d395bdc21346de") }
Display all documents from a collection with the help of find() method −
> db.demo197.find();
This will produce the following output −
{ "_id" : ObjectId("5e3afde803d395bdc21346d8"), "Name" : "Chris" } { "_id" : ObjectId("5e3afdef03d395bdc21346d9"), "Name" : "Bob" } { "_id" : ObjectId("5e3afdf203d395bdc21346da"), "Name" : "David" } { "_id" : ObjectId("5e3afdf603d395bdc21346db"), "Name" : "Sam" } { "_id" : ObjectId("5e3afdf903d395bdc21346dc"), "Name" : "Mike" } { "_id" : ObjectId("5e3afe1603d395bdc21346dd"), "Name" : "Carol" } { "_id" : ObjectId("5e3afe2003d395bdc21346de"), "Name" : "John" }
Following is the query using LIMIT() −
> db.demo197.find().limit(4);
This will produce the following output −
{ "_id" : ObjectId("5e3afde803d395bdc21346d8"), "Name" : "Chris" } { "_id" : ObjectId("5e3afdef03d395bdc21346d9"), "Name" : "Bob" } { "_id" : ObjectId("5e3afdf203d395bdc21346da"), "Name" : "David" } { "_id" : ObjectId("5e3afdf603d395bdc21346db"), "Name" : "Sam" }
Advertisements