- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $listLocalSessions
$listLocalSessions¶
On this page
Definition¶
- 
$listLocalSessions¶
- New in version 3.6. - Lists the sessions cached in memory by the - mongodor- mongosinstance.- Important - When a user creates a session on a - mongodor- mongosinstance, the record of the session initially exists only in-memory on the instance; i.e. the record is local to the instance. Periodically, the instance will sync its cached sessions to the- system.sessionscollection in the- configdatabase, at which time, they are visible to- $listSessionsand all members of the deployment. Until the session record exists in the- system.sessionscollection, you can only list the session via the- $listLocalSessionsoperation.- The - $listLocalSessionsoperation uses the- db.aggregate()method and not the- db.collection.aggregate().- To run - $listLocalSessions, it must be the first stage in the pipeline.- The stage has the following syntax: - The - $listLocalSessionsstage takes a document with one of the following contents:- { }- If running with access control, returns all sessions for the current authenticated user. - If running without access control, returns all sessions. - { users: [ { user: <user>, db: <db> }, ... ] }- Returns all sessions for the specified users. If running with access control, the authenticated user must have privileges with - listSessionaction on the cluster to list sessions for other users.- { allUsers: true }- Returns all sessions for all users. If running with access control, the authenticated user must have privileges with - listSessionaction on the cluster.
Restrictions¶
$listLocalSessions is not allowed in transactions.
Examples¶
List All Local Sessions¶
From the connected mongod/mongos instance’s
in-memory cache of sessions, the following aggregation operation lists
all sessions:
Note
If running with access control, the current user must have
privileges with listSession action on the cluster.
List All Local Sessions for the Specified Users¶
From the connected mongod/mongos instance’s
in-memory cache, the following aggregation operation lists all the
sessions for the specified user myAppReader@test:
Note
If running with access control and the current user is not the
specified user, the current user must
have privileges with listSession action on the cluster.