当超过100个结果时,Node.js +猫鼬查找会冻结节点 [英] Node.js + mongoose find freezes node when more than 100 results

查看:58
本文介绍了当超过100个结果时,Node.js +猫鼬查找会冻结节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的猫鼬模型,在此模型上,我将find限制为max 100,并调用完成的回调:

I have a simple mongoose model on which I call find with limit max 100 it calls the done callback:

this.find({}).limit(100).exec(done);

如果我将此行修改为(或更高的数字),则永远不会调用回调

The callback is never called If I modify this line into (or any higher number)

this.find({}).limit(101).exec(done);

任何地方都没有错误,数据库可以正常工作,但是此节点应用程序冻结,必须重新启动.

There is no error anywhere, the database keeps working, but this node app freezes and must be restarted.

如果我进入服务器以连接到相同的数据库并连接到mongo shell,则在同一集合上,find({})将在不到一秒的时间内返回所有〜700个集合. 当我将同一数据库克隆到本地PC并运行该应用程序以连接到本地数据库时,它可以正常工作,但是如果该应用程序连接到同一服务器上的数据库,则该应用程序将在服务器上冻结.

If I ssh into the server to connect to the same database and connect to mongo shell, on the same collection find({}) returns all ~700 collections in less than a sec. When I cloned the same database to my local PC and run the app to connect to local database it worked, but the app freezes on the server if its connect to the database on the same server.

有什么想法要调试吗?

Edit1:添加了模型文件:

Added model file:

模型文件:

'use strict';

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

let foodSchema = new Schema(
    {
        name: Object,
        type: String,
        description: Object,
        price: Number,
        priceBig: Number,
        active: Boolean
    },
    {
        collection: 'foods'
    }
);

let model = mongoose.model('food', foodSchema);

model.getAllFoods = function (done) {
    this.find({}, done);
};

model.getActiveFoods = function (done) {
    this.find({active: true}, done);
};

model.getFoodById = function (id, done) {
    this.findOne({_id: id}, done);
};

module.exports = model;

用法:

foodModel.getAllFoods(function (err, docs) {
    if (err) {
        res.sendStatus(500);
        return;
    }

    res.send(docs);
});

getActiveFoods正常工作(返回96个文档)

getActiveFoods works just fine (returns 96 docs)

推荐答案

JohnnyK提出提示后,我将Mongoose从4.1.11更新为4.3.7,从而解决了该问题.

After the tip from JohnnyK I updated Mongoose from 4.1.11 to 4.3.7 and that fixed the issue.

这篇关于当超过100个结果时,Node.js +猫鼬查找会冻结节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆