---
title: Querying connection information in MongoDB
description: As with all other database platforms, daily administration and troubleshooting of MongoDB often begins with analyzing database activity. It can be helpful
---

[Blog | Pythian ](https://www.pythian.com/blog)

# [Querying connection information in MongoDB](https://www.pythian.com/blog/querying-connection-information-in-mongodb)

 Written by [Pythian Marketing](https://www.pythian.com/blog/author/pythian-marketing) | May 21, 2012 4:00:00 AM

As with all other database platforms, daily administration and troubleshooting of **MongoDB** often begins with analyzing database activity. It can be helpful to see all connections from within MongoDB, which you can do by running:

`db.$cmd.sys.inprog.findOne({$all:1});` or `db.currentOp(true);`

where the “true” makes the command include idle connections.

## Managing High Volumes of Connection Data

But often there's so many connections that unless you capture the output in a script file or some similar thing, it's kind of useless. Thanks to a hint given by **Scott Hernandez** in the mongodb-user forum, we can use the line:

`db.currentOp(true).inprog.forEach(function(o){if( ) printjson(o)});`

to show a subset that is more manageable.

## Filtering Connections by Application Server or ConnectionID

For instance, we might want to show all connections from a particular application server:

`db.currentOp(true).inprog.forEach(function(o){if(o.client.indexOf(“10.0.1.77”) != -1 ) printjson(o)});`

Or from the MongoDB logs we’ll see a particular `connectionId` and want to know where it came from:

`db.currentOp(true).inprog.forEach(function(o){if(o.connectionId == 8606 ) printjson(o)});`

## Analyzing Connection Metadata Output

This will then show all the connection info for that `connectionId`:

```
{    "opid" : 7117752,    "active" : false,    "lockType" : "read",    "waitingForLock" : false,    "op" : "query",    "ns" : "player_data.player_answerchoice",    "query" : {      "$query" : {        "poll_id" : ObjectId("4f58e08db3e93217c2000008")      }    },    "client" : "10.0.1.77:59542",    "desc" : "conn",    "threadId" : "0x49e31940",    "connectionId" : 8606,    "numYields" : 0  } 
```

## MongoDB Database Consulting Services

Ready to optimize your MongoDB Database for the future?

 

[View full post](https://www.pythian.com/blog/querying-connection-information-in-mongodb)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Pythian Marketing"
  },
  "dateModified" : "2026-04-05T05:11:16.188Z",
  "datePublished" : "2012-05-21T04:00:00Z",
  "headline" : "Querying connection information in MongoDB",
  "image" : {
    "@type" : "ImageObject",
    "height" : 60,
    "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
    "width" : 60
  },
  "mainEntityOfPage" : "https://www.pythian.com/blog/querying-connection-information-in-mongodb",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "Pythian Blog"
  }
}
```