$listSessions

Definition

  • $listSessions

New in version 3.6.

Lists all sessions stored in the system.sessions collection in the config database.These sessions are visible to all members of the MongoDB deployment.

Important

When a user creates a session on a mongod ormongos instance, the record of the session initiallyexists only in-memory on the instance; i.e. the record is localto the instance. Periodically, the instance will sync its cachedsessions to the system.sessionscollection in the config database, at which time, they arevisible to $listSessions and all members of thedeployment. Until the session record exists in thesystem.sessions collection, you can only list the session viathe $listLocalSessions operation.

To run $listSessions, it must be the first stage in the pipeline.

The stage has the following syntax:

  1. { $listSessions: <document> }

The $listSessions stage takes a document with oneof the following contents:

{ }If running with access control, returns all sessions for thecurrent authenticated user.

If running without access control, returns all sessions.{ users: [ { user: <user>, db: <db> }, … ] }Returns all sessions for the specified users. If running withaccess control, the authenticated user must have privilegeswith listSession action on the cluster to listsessions for other users.{ allUsers: true }Returns all sessions for all users. If running with accesscontrol, the authenticated user must have privileges withlistSession action on the cluster.

Restrictions

$listSessions is not allowed in transactions.

Examples

List All Sessions

From the system.sessions collection,the following aggregation operation lists all sessions:

Note

If running with access control, the current user must haveprivileges with listSession action on the cluster.

  1. use config
  2.  
  3. db.system.sessions.aggregate( [ { $listSessions: { allUsers: true } } ] )

List All Sessions for the Specified Users

From the system.sessions collection,the following aggregation operation lists all sessions for thespecified user myAppReader@test:

Note

If running with access control and the current user is not thespecified user, the current user musthave privileges with listSession action on the cluster.

  1. use config
  2.  
  3. db.system.sessions.aggregate( [ { $listSessions: { users: [ {user: "myAppReader", db: "test" } ] } } ] )

List All Sessions for the Current User

From the system.sessions collection,the following aggregation operation lists all sessions for the currentuser if run with access control:

  1. use config
  2.  
  3. db.system.sessions.aggregate( [ { $listSessions: { } } ] )

If run without access control, the operation lists all sessions.