如何使用MongoDB本机Node.js驱动程序运行db.killOp()? [英] How to run db.killOp() using the MongoDB native Node.js driver?

查看:85
本文介绍了如何使用MongoDB本机Node.js驱动程序运行db.killOp()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB本机Node.js驱动程序1.4.38.

我已经使用以下命令进行了所有运行操作:

var maxSecsRunning = 2;
db.collection('$cmd.sys.inprog').findOne(function (err, data) {
                if (err) {
                    throw err;
                }
                if (data && data.inprog) {
                    data.inprog.forEach(function (op) {
                        console.log("Record", op);

                        if (op.secs_running > maxSecsRunning && op.op == "query" && !op.ns.indexOf("local") > -1) {
                            console.log("Killing opId: " + op.opid + " running for over secs: " + op.secs_running);
                            db.killOp(op.opid);


                        }

                    })
                }


            });

我想终止长时间运行的操作,但是db.killOp给出了错误:undefined不是一个函数.

如何在node.js MongoDB本机驱动程序中运行killOp?

解决方案

对于在db.collection('$cmd.sys.inprog')集合上运行查询的方式相同,您可以对db.collection('$cmd.sys.killop')集合上的db.killOp()执行相同的操作.

按照以下示例进行操作即可:

var maxSecsRunning = 2;
db.collection('$cmd.sys.inprog').findOne(function (err, data) {
    if (err) throw err;
    if (data && data.inprog) {
        data.inprog.forEach(function (op) {
            console.log("Record", op);
            if (op.secs_running > maxSecsRunning && 
                op.op == "query" && 
                !op.ns.indexOf("local") > -1) {
                console.log("Killing opId: " + op.opid 
                                             + " running for over secs: " 
                                             + op.secs_running);
                // same thing as db.killOp(op.opid)
                db.collection('$cmd.sys.killop')
                  .findOne({ 'op': op.opid }, function (err, data) {
                        if (err) throw err;
                        // do something with the result
                  });
            }
        });
    }
});

I am using MongoDB native Node.js Driver 1.4.38.

I have got all running operation using :

var maxSecsRunning = 2;
db.collection('$cmd.sys.inprog').findOne(function (err, data) {
                if (err) {
                    throw err;
                }
                if (data && data.inprog) {
                    data.inprog.forEach(function (op) {
                        console.log("Record", op);

                        if (op.secs_running > maxSecsRunning && op.op == "query" && !op.ns.indexOf("local") > -1) {
                            console.log("Killing opId: " + op.opid + " running for over secs: " + op.secs_running);
                            db.killOp(op.opid);


                        }

                    })
                }


            });

I want to kill long running operation but db.killOp is giving error : undefined is not a function.

How can I run killOp in node.js MongoDB native Driver ?

解决方案

The same way you ran the query on the db.collection('$cmd.sys.inprog') collection, you can do the same for the db.killOp() on the db.collection('$cmd.sys.killop') collection.

Following this example will do the trick:

var maxSecsRunning = 2;
db.collection('$cmd.sys.inprog').findOne(function (err, data) {
    if (err) throw err;
    if (data && data.inprog) {
        data.inprog.forEach(function (op) {
            console.log("Record", op);
            if (op.secs_running > maxSecsRunning && 
                op.op == "query" && 
                !op.ns.indexOf("local") > -1) {
                console.log("Killing opId: " + op.opid 
                                             + " running for over secs: " 
                                             + op.secs_running);
                // same thing as db.killOp(op.opid)
                db.collection('$cmd.sys.killop')
                  .findOne({ 'op': op.opid }, function (err, data) {
                        if (err) throw err;
                        // do something with the result
                  });
            }
        });
    }
});

这篇关于如何使用MongoDB本机Node.js驱动程序运行db.killOp()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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