简体   繁体   English

如何在 MongoDB 中正确创建索引以在整个集合中进行搜索?

[英]How to create index properly in MongoDB to search in entire one collection?

I a have collection named people and this collection have almost 100k documents ..我有一个名为people集合,这个集合有近10 万个documents ..

While I am a front-end dev, I don't know How can I boost my search performance.虽然我是前端开发人员,但我不知道如何提高搜索性能。

I do search as below:我搜索如下:

const phoneNumbers = [
   { "phone": { "$regex": `1` } },
   { "phone": { "$regex": `2` } },
   { "phone": { "$regex": `3` } },
   { "phone": { "$regex": `4` } },
   { "phone": { "$regex": `5` } },
   { "phone": { "$regex": `xxx` } },
   { "phone": { "$regex": `999` } },
   { "phone": { "$regex": `1000` } },
];


peopleCollection.find({$or: phoneNumbers}).toArray((error, matchedDocs)=> {
   // returning matched docs
});

As you can see the structure of my search what is your suggestions to create index ?正如您所看到的我的搜索结构,您对创建index什么建议?

Is that helpful to create index ?这对创建索引有帮助吗? If yes how Can I create a proper index ?如果是,我如何创建适当的索引

Indexing does not reduce the complexity of your search expression, rather it helps the database to find out the matched result faster(ie reduce the time complexity).索引不会降低搜索表达式的复杂度,而是帮助数据库更快地找出匹配的结果(即降低时间复杂度)。

Here is how you can create indexing:以下是创建索引的方法:

db.collectionName.createIndex(key_name) db.collectionName.createIndex(key_name)

The above one-line operation creates indexing on key key_name .上面的单行操作在键key_name上创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM