javascript - 如何在 MongoDB 中正确创建索引以搜索整个集合?
问题描述
我有一个名为的集合people
,这个集合有近100k 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上创建索引。
推荐阅读
- avro - AVRO,将记录转换为数组
- tensorflow - 如何在 Keras 中实现屏蔽的 softmax 交叉熵损失函数?
- c# - 为什么作为消息发送后 DateTime 变量的默认值会更改?
- c - 如何停止输入换行符
- flutter - 多个颤振模拟器实例
- amazon-web-services - 外部视图查询在 athena 控制台中工作,但在 aws quicksight 中使用时无法工作
- python - 使用循环绘制具有自定义线格式的大型数据集的强大解决方案
- c++ - 在 OMNET++ 中接收到上一个模块的所有消息后如何将一个消息发送到下一个模块
- r - 基于多个条件创建新变量的问题
- typescript - 使用引用将打字稿文件加载到另一个打字稿文件中