简体   繁体   English

在具有数百万条记录的数据库(mongoDB)中搜索需要超过 1 分钟才能检索数据

[英]searching in database (mongoDB) with millions of records takes more than 1 minute to retrieve data

I'm using node js and while searching for a field it takes over 1 minute or more to get the data back is there any solutions to increase performance and make it faster我正在使用节点 js,在搜索字段时需要 1 分钟或更长时间才能取回数据是否有任何解决方案可以提高性能并使其更快

here is my code:这是我的代码:

router.get('/employee', (req,res) => {
    let searchQuery = {name: req.query.name};

    Employee.findOne(searchQuery)
        .then(employee => {
            res.render('search',{employee:employee});
            
        })
        .catch(err => {
            req.flash('error_msg', 'ERROR: '+err)
            res.redirect('/');
        })
}); 

Schema:架构:

const mongoose = require('mongoose');

let employeeScheme = new mongoose.Schema({
    name: String,
    postion: String,
    salary: String
});
module.exports = mongoose.model('Employee', employeeScheme);

Yeap.是的。 You can create an index for fields you normally search for.您可以为通常搜索的字段创建索引。

Exemple:示例:

employeeScheme.index({ name: 1, type: 1 })

Read the documentation for choose the best index type for you.阅读文档以选择最适合您的索引类型。 https://mongoosejs.com/docs/guide.html#indexes https://mongoosejs.com/docs/guide.html#indexes

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

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