0
我在mongodb中的一個集合中有大約300k個文檔,這都是坐在AWS中。當我查詢這個特定的路由(thing2)時,看起來mongo處理太多了,所以mongod服務崩潰了。我已經爲我的代碼添加了限制,但它仍然無法工作。理想情況下,我希望能夠搜索特定關鍵字,但該部分現在已被註釋掉。Nodejs代碼崩潰Mongodb服務器
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' }
編輯:蒙戈日誌
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 }
你不需要(不應該)連接所有的時間你有一個請求。您可以在開始時連接一次,並在整個應用程序中繼續使用此連接。 –
@ israel.zinc我將如何解決這個問題呢? – user1883614
當服務崩潰時,mongo日誌會說什麼?你在納米實例嗎? Mongo通常很穩固 – Matt