To reduce the time of finding records in MongoDB, you can use indexes. Following is the syntax -
db.yourCollectionName.createIndex({yourFieldName:1});
You can follow the following methods to create index for field names based on numbers, text, hash, etc.
Let’s create an index. Following is the query-
> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
To understand the above concept, let us create another index-
> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
Now let us create another index -
> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
The above is the detailed content of What to do when MongoDB takes too long to look up records?. For more information, please follow other related articles on the PHP Chinese website!