简体   繁体   English

猫鼬查询阻止Node.js

[英]Mongoose query blocks Node.js

Mongoose blocks Node.js when it is getting data. 猫鼬在获取数据时会阻塞Node.js。 I thought that it is supposed to be absolutely no blocking and when callback appears then it should just get back there. 我认为应该绝对没有阻塞,并且当回调出现时,它应该回到那里。

The problem is with: 问题在于:

Container.find({}, function (err, documents) {
        res.status(200).send(documents);
});

When I will run this route in ExpressJS it will just freeze NodeJS for around 10sec, and no one else can reach connection then. 当我在ExpressJS中运行此路由时,它将冻结NodeJS 10秒钟左右,然后其他任何人都无法建立连接。

I'm having a open connection to MongoDB at the start using Mongoose and not doing anything with it later on. 我从一开始就使用Mongoose建立了到MongoDB的开放连接,以后不再对其进行任何处理。 What's the problem? 有什么问题? It is supposed to work like that? 应该这样工作吗?

UPDATE: 更新:

So this is how I init mongoose 这就是我初始化猫鼬的方式

function initialDb() {
seed();
seedStructure();
startApplication();
}

database.connect();
database.loadModels(initialDb);

and this is the place where i connect and init models 这是我连接和初始化模型的地方

import mongoose from 'mongoose';
import chalk from 'chalk';
import config from '../config';

export default {
    loadModels(callback){
        require('../../models/site');
        require('../../models/page');
        require('../../models/container');
        require('../../models/template');
        require('../../models/theme');

        if (typeof callback === 'function') {
            return callback();
        }
    },

    connect () {
        mongoose.connect(config.db.uri, function (err) {
            if (err) {
                console.error(chalk.red('Could not connect to MongoDB!'));
                console.log(err);
            }
        });
    },

    disconnect(callback) {
        mongoose.disconnect(function (err) {
            console.info(chalk.yellow('Disconnected from MongoDB.'));
            callback(err);
        });
    }
};

and the model 和模型

var mongoose = require('mongoose');
var Schema = mongoose.Schema;



var container = new Schema({

});


let model = mongoose.model('container', container);


module.exports = model;

It returns around 26k documents. 它返回大约26k文档。

Ok so basically I found out, that if I will stream that instead of getting it with one callback it will work way better (I will be able to get into some other actions) 好的,所以基本上我发现,如果我将其流式传输而不是通过一个回调进行获取,它将更好地工作(我将能够进行其他一些操作)

like this 像这样

var stream = Container.find({}).stream();

var array = [];
stream.on('data', (doc) => {
    array.push(doc);
}).on('error', (err) => {

}).on('close', () => {
    res.send(array);
});

It will solve the problem. 它将解决问题。 So this is how I would get big data from mongodb, though why it is slowing so much if I will get it in one callback? 因此,这就是我从mongodb获取大数据的方式,尽管如果我将其通过一次回调获取它,为什么它会这么慢? Due to 12MB data? 由于有12MB​​的数据? Big json that needs to be parsed or what? 需要解析的大json或什么? Cause this is a quite mysterious for me (the reason for slow down) 因为这对我来说是一个非常神秘的东西(减速的原因)

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

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