Nodejs代码崩溃Mongodb服务器

时间:2017-04-19 01:30:18

标签: node.js mongodb amazon-web-services

我在mongodb的一个系列中有大约300,000个文档,而且这些文档都在AWS中。当我查询这个特定的路由(thing2)时,似乎mongo的处理太多了,所以mongod服务崩溃了。我已经为我的代码添加了一个限制,但它仍然无效。理想情况下,我希望能够搜索特定的关键字,但该部分目前已被注释掉。

var express = require('express')
var app = express()

app.get('/', function (req, res) {
    res.send('Hello World!')
});

app.get('/thing2', function(req, res) {

    var username = req.query.username
    var keyword = req.query.keyword

    var MongoClient = require('mongodb').MongoClient,
        assert = require('assert');
    var url = 'mongodb://localhost:27017/my_db';

    MongoClient.connect(url, function(err, db) {
        if (err) throw err;

        var collection = db.collection('my_collection');
        / * collection.find({ $and: [ 
          * {"text" : {$regex : ".*"+keyword+".*"}}, 
          *  {"username" : username}
          *  ] }).limit(5).toArray(function(err, docs) {
          */

        collection.find({"user.screen_name" : username}).limit(1).toArray(function(err, docs) {
            console.log("\n" + new Date().toLocaleString() + " " + username);

            if (!err) {
                console.log(docs.length)
            } else {
                console.log(err)
            }

            res.send(docs)
        });
    });
});

app.listen(80, function () {
    console.log('Listening on port 80!')
});

我在控制台中看到的错误是:

{ [MongoError: connection 0 to localhost:27017 closed]
  name: 'MongoError',
  message: 'connection 0 to localhost:27017 closed' }

编辑:mongo log

2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] 
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] 
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2017-04-19T03:59:03.458+0000 I CONTROL  [initandlisten] 
2017-04-19T03:59:03.471+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/var/lib/mongodb/diagnostic.data'
2017-04-19T03:59:03.471+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2017-04-19T03:59:03.471+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-04-19T03:59:04.003+0000 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK
2017-04-19T03:59:23.504+0000 I COMMAND  [ftdc] serverStatus was very slow: { after basic: 40, after asserts: 110, after connections: 130, after extra_info: 160, after globalLock: 310, after locks: 540, after network: 630, after opcounters: 760, after opcountersRepl: 870, after storageEngine: 1120, after tcmalloc: 1540, after wiredTiger: 2690, at end: 3370 }

1 个答案:

答案 0 :(得分:0)

问题是你经常连接。我会使用mongoose重做代码:

var express = require('express')
var app = express()
var mongoose   = require('mongoose');
mongoose.connect('mongodb://localhost/my_db')
var collection = require('user');

app.get('/', function (req, res) {
    res.send('Hello World!')
});

app.get('/thing2', function(req, res) {

    var username = req.query.username
    var keyword = req.query.keyword

    collection.find({"user.screen_name" : username}).limit(1).toArray(function(err, docs) {
            console.log("\n" + new Date().toLocaleString() + " " + username);

            if (!err) {
                console.log(docs.length)
            } else {
                console.log(err)
            }
            res.send(docs)
    });
});

app.listen(80, function () {
    console.log('Listening on port 80!')
});

你可以像这样建模一个猫鼬模式(文件的名称是:“user.js”):

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

var UserSchema   = new Schema({
    name: String,
    userId : Number
});
module.exports = mongoose.model('User', UserSchema);

尝试使其工作最小化,然后随着真实模型增加。 希望我的回答很有帮助