
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Why is MongoDB taking too much time to find the record?
In this case, use the concept of index on a particular field. Let us first create a collection with documents. Here, we have created index as well using createIndex() −
> db.decreasetimeusingindex.createIndex({"StudentName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.decreasetimeusingindex.insertOne({"StudentName":"John Smith","StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e03960af5e889d7a51994ff") } > db.decreasetimeusingindex.insertOne({"StudentName":"John Doe","StudentAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e039611f5e889d7a5199500") } > db.decreasetimeusingindex.insertOne({"StudentName":"Adam Smith","StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e03961df5e889d7a5199501") }
Following is the query to display all documents from a collection with the help of find() method −
> db.decreasetimeusingindex.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e03960af5e889d7a51994ff"), "StudentName" : "John Smith", "StudentAge" : 21 } { "_id" : ObjectId("5e039611f5e889d7a5199500"), "StudentName" : "John Doe", "StudentAge" : 24 } { "_id" : ObjectId("5e03961df5e889d7a5199501"), "StudentName" : "Adam Smith", "StudentAge" : 22 }
Following is the query to find a specific record −
> db.decreasetimeusingindex.find({"StudentName":"Adam Smith"});
This will produce the following output −
{ "_id" : ObjectId("5e03961df5e889d7a5199501"), "StudentName" : "Adam Smith", "StudentAge" : 22 }
- Related Articles
- What is to be done when MongoDB takes too much time to find the record?
- Fiber: How Much Is Too Much?
- Why do I sweat too much, Is it bad for me?
- The World is too Much With Us
- Connecting SAP B1 via DI API takes too much time
- How can I check how much time MySQL query, without printing it on the console, is taking?
- How to find a record by _id in MongoDB?
- Are You Sleeping Too Much? Here’s How to Tell
- Drinking Too Much Water (Hyponatremia): What You Need to Know
- How do you find a MongoDB record that is two level deep?
- MongoDB query to find a specific city record from a collection
- Joining cobk and coep table using Inner join taking too long
- Are You Exercising Too Much? Here's How to Tell
- What happens in villages when it rains too much?
- If friction is due to roughness, then why is too much force required to move a rubber piece on smooth glass, even though both the surfaces are smooth?

Advertisements