繁体   English   中英

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

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

我有一个名为people集合,这个集合有近10 万个documents ..

虽然我是前端开发人员,但我不知道如何提高搜索性能。

我搜索如下:

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
});

正如您所看到的我的搜索结构,您对创建index什么建议?

这对创建索引有帮助吗? 如果是,我如何创建适当的索引

索引不会降低搜索表达式的复杂度,而是帮助数据库更快地找出匹配的结果(即降低时间复杂度)。

以下是创建索引的方法:

db.collectionName.createIndex(key_name)

上面的单行操作在键key_name上创建索引。

暂无
暂无

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

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