drkjzk3359 2019-06-10 15:10
浏览 164

如何使用mongoose或mongodb客户端关闭来自节点js的mongodb连接

there are approximately 21000 connections are there for mongodb which are stale connections.

i have tried "currentOp" command to list all the connections. i was unable to close from node js using mongoose.

const express = require('express');
var app = express();
const mongoose = require('mongoose'),
Admin = mongoose.mongo.Admin;
const port = 3002
mongoose.set('debug', true)
var db = mongoose.createConnection('mongodb://localhost:22222/myapp', { 
useNewUrlParser: true }, () => {
  console.log('Connection db established')
})
app.use((req, res, next) => {
    console.log(req.url + ' ' + new Date());
    next();
});
const Cat = mongoose.model('cats', { name: String });

db.on('open', function (err, data) {


    new Admin(db.db).command({ currentOp: 1, '$all': true }, (err, r) => {
        var connections = []
        console.log(err)
        console.log(r.inprog.length)
        r.inprog.forEach((conn) => {
            if (conn.client) {
                console.log(conn.client)
                connections.push({ thread: conn.threadId, connection: conn.connectionId, op: conn.opid })
            }
        })
        console.log(connections)
    })
});

app.listen(port, () => {
    console.log('Server started')
})

i have used "killOp" to kill connection but didn't work. and there is no "opid" for stale connections to kill or to identify

  • 写回答

1条回答 默认 最新

  • dpvm7231 2019-06-10 15:45
    关注

    Your mongoose connection can be closed with

    mongoose.connection.close()
    

    Make sure to put in the correct callback however, so that your database gets saved before the connection closes.

    You can read more in the docs found here.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题