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