Skip to content

Latest commit

 

History

History

change-streams-monitor

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

change-stream-monitor

Index

This snippet allows mongosh users to monitor Change Streams on the current server.

On installation of this snippet, the following are available to the user.

listChangeStreams(extended?: boolean, allUsers?: boolean, nsFilter?: Array)

Prints a table with the currently open Change Streams. Note that the table resizes itself based on the size of the terminal.

The behaviour of the function can be controlled with the available parameters (see parameter defaults for default behaviour). See prettyPrintChangeStreamPipeline() to pretty print a change stream pipeline. See ChangeStreamsData and ExtendedChangeStreamsData for data outputted in extended and non-extended mode.

  • extended - Controls whether a simple or extended output is presented. Refer to ExtendedChangeStreamsData. Defaults to false.
  • allUsers - Boolean that correspond's to the allUsers flag of the $currentOp MongoDB Pipeline Stage i.e. If set to false, $currentOp only reports on operations/idle connections/idle cursors/idle sessions belonging to the user who ran the command. If set to true, $currentOp reports operations belonging to all users. Defailts to true.
  • nsFilter - An optional array of namespace filter. Defaults to [] i.e. to filter.
Column Name Extended Output Description
ConnID No An identifier for the connection where the specific operation originated.
AppName No The identifier of the client application which ran the operation. Use the appName connection string option to set a custom value for the appName field.
Remote No The IP address (or hostname) and the ephemeral port of the client connection where the operation originates.
Driver No The MongoDB Driver used to connect and run the Change Stream.
NS No The namespace the operation targets. A namespace consists of the database name and the collection name concatenated with a dot (.); that is, ".".
Type No The type of operation. Values are either: op / idleSession / idleCursor.
Pipeline No The Change Stream pipeline. Use prettyPrintChangeStreamPipeline(connId) to pretty print the full pipeline.
LastAccessDate No The date and time when the cursor was last used.
Docs Returned No The cumulative number of documents returned by the cursor.
Active Yes A boolean value specifying whether the operation has started.
User Yes Users associated with the operation
CursorId Yes The ID of the cursor.
CreatedDate Yes The date and time when the cursor was created.

Sample Output - Normal Mode

replset [primary] test> listChangeStreams()
  ┏━━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━┓
  ┃  ConnID   ┃  AppName   ┃  Remote      ┃  Driver      ┃  NS          ┃  Type  ┃  Pipeline    ┃  LastAccess  ┃  DocsReturn  ┃
  ┃           ┃            ┃              ┃              ┃              ┃        ┃              ┃  Date        ┃  ed          ┃
  ┡━━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━┩
  │  74       │  cs2       │  127.0.0.1:  │  mongo-java  │  test.event  │  op    │  [           │  "2024-04-2  │  0           │
  │           │            │  54989       │  -driver|sy  │  s           │        │  {           │  2T13:23:10  │              │
  │           │            │              │  nc: 4.9.1   │              │        │  "$changeSt  │  .160Z"      │              │
  │           │            │              │              │              │        │  ream": {}   │              │              │
  │           │            │              │              │              │        │  },          │              │              │
  │           │            │              │              │              │        │  {           │              │              │
  │           │            │              │              │              │        │  "$match":   │              │              │
  │           │            │              │              │              │        │  {           │              │              │
  │           │            │              │              │              │        │  "operation  │              │              │
  │           │            │              │              │              │        │  Type": {    │              │              │
  │           │            │              │              │              │        │  "$in": [    │              │              │
  │           │            │              │              │              │        │  "insert",   │              │              │
  │           │            │              │              │              │        │  "update"    │              │              │
  │           │            │              │              │              │        │  ]           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  ]           │              │              │
  ├───────────┼────────────┼──────────────┼──────────────┼──────────────┼────────┼──────────────┼──────────────┼──────────────┤
  │  79       │  cs1       │  127.0.0.1:  │  mongo-java  │  test.event  │  op    │  [           │  "2024-04-2  │  0           │
  │           │            │  55011       │  -driver|sy  │  s           │        │  {           │  2T13:23:10  │              │
  │           │            │              │  nc: 4.9.1   │              │        │  "$changeSt  │  .181Z"      │              │
  │           │            │              │              │              │        │  ream": {}   │              │              │
  │           │            │              │              │              │        │  },          │              │              │
  │           │            │              │              │              │        │  {           │              │              │
  │           │            │              │              │              │        │  "$match":   │              │              │
  │           │            │              │              │              │        │  {           │              │              │
  │           │            │              │              │              │        │  "operation  │              │              │
  │           │            │              │              │              │        │  Type": {    │              │              │
  │           │            │              │              │              │        │  "$in": [    │              │              │
  │           │            │              │              │              │        │  "insert",   │              │              │
  │           │            │              │              │              │        │  "update"    │              │              │
  │           │            │              │              │              │        │  ]           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  }           │              │              │
  │           │            │              │              │              │        │  ]           │              │              │
  └───────────┴────────────┴──────────────┴──────────────┴──────────────┴────────┴──────────────┴──────────────┴──────────────┘
Found 2 change streams

Sample Output - Extended

replset [primary] test> listChangeStreams(true)
  ┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┓
  ┃  ConnID  ┃  AppNam  ┃  Remote  ┃  Driver  ┃  NS      ┃  Type  ┃  Pipeli  ┃  LastAc  ┃  DocsRe  ┃  Active  ┃  User    ┃  Cursor  ┃  Create  ┃
  ┃          ┃  e       ┃          ┃          ┃          ┃        ┃  ne      ┃  cessDa  ┃  turned  ┃          ┃          ┃  Id      ┃  dDate   ┃
  ┃          ┃          ┃          ┃          ┃          ┃        ┃          ┃  te      ┃          ┃          ┃          ┃          ┃          ┃
  ┡━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━┩
  │  74      │  cs2     │  127.0.  │  mongo-  │  test.e  │  op    │  [       │  "2024-  │  0       │  true    │  john@a  │  754369  │  "2024-  │
  │          │          │  0.1:54  │  java-d  │  vents   │        │  {       │  04-22T  │          │          │  dmin    │  716098  │  04-22T  │
  │          │          │  989     │  river|  │          │        │  "$chan  │  13:24:  │          │          │          │  703700  │  12:15:  │
  │          │          │          │  sync:   │          │        │  geStre  │  25.528  │          │          │          │  0       │  31.896  │
  │          │          │          │  4.9.1   │          │        │  am":    │  Z"      │          │          │          │          │  Z"      │
  │          │          │          │          │          │        │  {}      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  },      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  {       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "$matc  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  h": {   │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "opera  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  tionTy  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  pe": {  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "$in":  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  [       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "inser  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  t",     │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "updat  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  e"      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  ]       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  ]       │          │          │          │          │          │          │
  ├──────────┼──────────┼──────────┼──────────┼──────────┼────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┼──────────┤
  │  79      │  cs1     │  127.0.  │  mongo-  │  test.e  │  op    │  [       │  "2024-  │  0       │  true    │  mary@a  │  697267  │  "2024-  │
  │          │          │  0.1:55  │  java-d  │  vents   │        │  {       │  04-22T  │          │          │  dmin    │  149292  │  04-22T  │
  │          │          │  011     │  river|  │          │        │  "$chan  │  13:24:  │          │          │          │  716100  │  12:16:  │
  │          │          │          │  sync:   │          │        │  geStre  │  25.542  │          │          │          │  0       │  01.889  │
  │          │          │          │  4.9.1   │          │        │  am":    │  Z"      │          │          │          │          │  Z"      │
  │          │          │          │          │          │        │  {}      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  },      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  {       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "$matc  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  h": {   │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "opera  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  tionTy  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  pe": {  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "$in":  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  [       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "inser  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  t",     │          │          │          │          │          │          │
  │          │          │          │          │          │        │  "updat  │          │          │          │          │          │          │
  │          │          │          │          │          │        │  e"      │          │          │          │          │          │          │
  │          │          │          │          │          │        │  ]       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  }       │          │          │          │          │          │          │
  │          │          │          │          │          │        │  ]       │          │          │          │          │          │          │
  └──────────┴──────────┴──────────┴──────────┴──────────┴────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┴──────────┘
Found 2 change streams

listChangeStreams.help()

Provides help on how to use the function.

prettyPrintChangeStreamPipeline(connectionId: any)

Pretty prints the Change Stream pipeline for a given Connection ID.

  • connectionId - The connection ID where the change stream is executing.

Example

replset [primary] test> prettyPrintChangeStreamPipeline(74)
[
  { '$changeStream': {} },
  {
    '$match': { operationType: { '$in': [ 'insert', 'update' ] } }
  }
]

prettyPrintChangeStreamPipeline.help()

Provides help on how to use the function.

ChangeStreamsData.help()

Describes the table output in normal mode.

ExtendedChangeStreamsData.help()

Describes the table output in extended mode.