- 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
What is to be done when MongoDB takes too much time to find the record?
To reduce the time to find record in MongoDB, you can use index. Following is the syntax −
db.yourCollectionName.createIndex({yourFieldName:1});
You can follow the below approaches to create index for field names based on number, text, hash, etc.
First Approach
Let us create an index. Following is the query −
> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Second Approach
To understand the above concept, let us create another index −
> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Third Approach
Let us now create another index −
> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Advertisements