Search code examples
mongodbmongo-shell

mongoDB output client driver name only from currentOp function


I'm in no way a mongoDB expert.
I try to get the currentOp.clientMetadata.driver for every currentOp currently running.
I know how to filter to get all the operation that have a field clientMetadata:

db.currentOp( { "clientMetadata":  {$exists: true } })

But this output all the operations with all there data which is verbose and useless.
I only want to filter the output to only some specific field like :

  • clientMetadata.driver
  • client
  • clientMetadata.application

Is there a way to only output some field from currentOp, I've tried find, in many ways but apparently db.currentOp is not to be found...


Solution

  • From official MongoDB doc, currentOp is deprecated

    Deprecated since version 6.2.

    In versions 6.2 and later use the $currentOp aggregation stage.

    In an aggregation pipeline, you can chain up the $currentOP operation with other projections like this:

    db.getSiblingDB("admin").aggregate([
        { 
            $currentOp: {} 
        },
        {
            $project: {
                // the fields you want here
                "clientMetadata.driver": 1,
                "client": 1,
                "clientMetadata.application": 1
            }
        }
    ])