Home>Article>Database> What to do when MongoDB takes too long to look up records?

What to do when MongoDB takes too long to look up records?

王林
王林 forward
2023-08-28 21:21:02 1198browse

当 MongoDB 查找记录花费太多时间时该怎么办?

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.

First method

Let’s create an index. Following is the query-

> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }

Second method

To understand the above concept, let us create another index-

> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }

Third method

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete