Listing Sessions by using db.currentOp()

502 views
Skip to first unread message

rohit reddy

unread,
Mar 12, 2018, 6:32:03 PM3/12/18
to mongodb-user
Hello,
I want to list the sessions connected to a mongos cluster using db.currentOp() method.  However, I want to display only selected columns from the outpot of db.currentOp().
I am a newbie to javascript and i come from a DBA background, so please pardon me if this question is too simple for most of you.

Below is the command i am using in mongodb Shell


var sess = db.currentOp()

sess.inprog.forEach(function(s) {
    //print('Session Connected to ############################' +s.shard);
    //print('Description ' +s.desc);
    //print('threadID is ' +s.threadID);
    print('Client Machine is' +s.client_s);
    print('Elapsed Time in Secs : ' +s.secs_running);
    print('Is Session Active :' +s.active);
    //print('Client.application: '+s.clientMetadata.application.name + 'Driver is: '+s.clientMetadata.driver.name + 'Client OS is:'+"s.clientMetadata.os.name");
    print('Client.application: '+s.clientMetadata.application.name);
    print('');
    print('');
    //print('End of Session #####################################')
}
);

However, when i run this command, i get the following errors:-

Client Machine isundefined
Elapsed Time in Secs : undefined
Is Session Active :true
2018-03-12T22:28:29.819+0000 E QUERY    [thread1] TypeError: s.clientMetadata is undefined :
@(shell):9:1
@(shell):1:1


MongoDB version is 3.6.2.

My guess here is that for the first element in the entry, there might not be a clientMetadata feild, However, i am not sure if there is any NVL function (I am familiar with ORacle NVL and DECODE functions) equivalent in mongoDB shell.

Please let me know if i am doing something incorrect here.

Thanks
Rohit Reddy

Kevin Adistambha

unread,
Mar 14, 2018, 1:47:44 AM3/14/18
to mongodb-user

Hi Rohit

The mongo shell is basically a Javascript shell, so most Javascript functions will work. This guide by Mozilla should be enough to get you started: https://developer.mozilla.org/en-US/docs/Web/JavaScript

You are correct that the gist of the error is the non-existence of the clientMetadata object. You would need to check if the object exists before attempting to do any operation on it.

For example, I have modified slightly your example code to be:

db.currentOp().inprog.forEach(function(s) { 
  print('Host: ' + s.host)
  print('Desc: ' + s.desc)
  if (typeof s.clientMetadata !== 'undefined') {
    print('Application: ' + JSON.stringify(s.clientMetadata.application.name))
    print('Driver: ' + JSON.stringify(s.clientMetadata.driver.name))
    print('OS: ' + JSON.stringify(s.clientMetadata.os.name))
  }
  print('')
});

This would output something similar to:

Host: localhost:27017
Desc: ReplBatcher

Host: localhost:27017
Desc: conn
Application: "MongoDB Shell"
Driver: "MongoDB Internal Client"
OS: "Mac OS X"

Host: localhost:27017
Desc: WT RecordStoreThread: local.oplog.rs

Host: localhost:27017
Desc: rsSync

Host: localhost:27017
Desc: NoopWriter

Host: localhost:27017
Desc: monitoring keys for HMAC

Best regards
Kevin

rohit reddy

unread,
Mar 14, 2018, 6:06:55 PM3/14/18
to mongodb-user
Thank you for your response and your code example. The link provided was great start for beginners like me.
 
Regards,
Rohit
Reply all
Reply to author
Forward
0 new messages