$listLocalSessions

Definition

  • $listLocalSessions

New in version 3.6.

Lists the sessions cached in memory by the mongod ormongos instance.

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.

The $listLocalSessions operation uses the db.aggregate()method and not the db.collection.aggregate().

To run $listLocalSessions, it must be the first stage inthe pipeline.

The stage has the following syntax:

  1. { $listLocalSessions: <document> }

The $listLocalSessions 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

$listLocalSessions is not allowed in transactions.

Examples

List All Local Sessions

From the connected mongod/mongos instance’sin-memory cache of sessions, the following aggregation operation listsall sessions:

Note

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

  1. db.aggregate( [ { $listLocalSessions: { allUsers: true } } ] )

List All Local Sessions for the Specified Users

From the connected mongod/mongos instance’sin-memory cache, the following aggregation operation lists all thesessions for the specified 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. db.aggregate( [ { $listLocalSessions: { users: [ { user: "myAppReader", db: "test" } ] } } ] )

List All Local Sessions for the Current User

From the connected mongod/mongos instance’sin-memory cache, the following aggregation operation lists all sessionsfor the current user if run with access control:

  1. db.aggregate( [ { $listLocalSessions: { } } ] )

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