Home > Database > MongoDB > body text

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

王林
Release: 2023-08-28 21:21:02
forward
1274 people have browsed it

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

To reduce the time of finding records in MongoDB, you can use indexes. Following is the syntax -

db.yourCollectionName.createIndex({yourFieldName:1});
Copy after login

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
}
Copy after login

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
}
Copy after login

Third method

Now let us create another index -

> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"});
{
   "createdCollectionAutomatically" : true,
   "numIndexesBefore" : 1,
   "numIndexesAfter" : 2,
   "ok" : 1
}
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!