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
}