MongoError:连接4到群集关闭

时间:2017-03-31 12:52:09

标签: node.js mongodb mongoose

我使用Mongoose驱动程序在NodeJS中有一个函数,如下所示:

伪代码:

function someFn(someParams) {
    // Step 1: a couple of very fast mongo queries (in milliseconds)
    // Step 2: HUGE CPU processing - think millions of data grouped, mapped, etc. (takes about a minute)
    // Step 3: another mongo query which inserts the results from Step 2 into a collection
}

在第3步,我收到以下错误:

MongoError: connection 4 to cluster closed
at Function.MongoError.create (/home/some-user/my-repo/node_modules/mongodb-core/lib/error.js:29:11)
at TLSSocket.<anonymous> (/home/some-user/my-repo/node_modules/mongodb-core/lib/connection/connection.js:202:22)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:101:20)
at TLSSocket.emit (events.js:191:7)
at _handle.close (net.js:513:12)
at Socket.done (_tls_wrap.js:332:7)
at Object.onceWrapper (events.js:293:19)
at emitOne (events.js:101:20)
at Socket.emit (events.js:191:7)
at TCP._handle.close [as _onclose] (net.js:513:12)

我的MongoDB连接参数如下:

mongoose.connect(connStr, {
    server: {
        socketOptions: {
            keepAlive: 300000,
            connectTimeoutMS: 300000,
            socketTimeoutMS: 300000,
            auto_reconnect: true
        }
    }
});

我不明白为什么我在第3步收到此错误。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

经过数小时的调试后找出问题。我的第3步mongoose查询包含太多文档(大约数百万,来自第2步)。来自mongoose的错误没有说明连接正在关闭的原因。像Too many documentsToo large query这样的邮件可以节省大量时间。