简体   繁体   中英

How to get current operations in mongodb using the nodejs driver

I am using node js as front end and mongodb is as backend. I got successfull connection. But while trying to get all the process running in mongodb are throw error. My code is below.

var Db = require('mongodb').Db, Server = require('mongodb').Server;
        var db = new Db('admin', new Server(conf.host, conf.port));
        db.open(function(err, db1) {
            var adminDb = db1.admin();
            adminDb.authenticate(conf.user, conf.password, function(err, result) {
                if(result == true) {
                     var obj = adminDb.currentOp({"active":true});
                     console.log("output="+output);
                }

            });
    });

I am getting below error.

TypeError: adminDb.currentOp is not a function

You want .command() :

   adminDb.command({ "currentOp": 1, "active": true },function(err,result) {
       // code in here
   });

If you are ever looking for how things are "actually" executed, then go into the mongo shell and type the function without the () parentheses on the end. This will show you the definition.

IE for currentOp() :

> db.currentOp

function (arg) {
        var q = {};
        if (arg) {
            if (typeof(arg) == "object")
                Object.extend(q, arg);
            else if (arg)
                q["$all"] = true;
        }

        var commandObj = {"currentOp": 1};
        Object.extend(commandObj, q);
        var res = this.adminCommand(commandObj);
        if (commandUnsupported(res)) {
            // always send legacy currentOp with default (null) read preference (SERVER-17951)
            var _readPref = this.getMongo().getReadPrefMode();
            try {
                this.getMongo().setReadPref(null);
                res = this.getSiblingDB("admin").$cmd.sys.inprog.findOne(q);
            } finally {
                this.getMongo().setReadPref(_readPref);
            }
        }
        return res;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM