簡體   English   中英

在具有數百萬條記錄的數據庫(mongoDB)中搜索需要超過 1 分鍾才能檢索數據

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

我正在使用節點 js,在搜索字段時需要 1 分鍾或更長時間才能取回數據是否有任何解決方案可以提高性能並使其更快

這是我的代碼:

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('/');
        })
}); 

架構:

const mongoose = require('mongoose');

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

是的。 您可以為通常搜索的字段創建索引。

示例:

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

閱讀文檔以選擇最適合您的索引類型。 https://mongoosejs.com/docs/guide.html#indexes

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM