Docs Menu
Docs Home
/
Database Manual
/ / /

$listLocalSessions (aggregation stage)

$listLocalSessions

Lists the sessions cached in memory by the mongod or mongos instance.

Important

When a user creates a session on a mongod or mongos instance, 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.sessions collection in the config database, at which time, they are visible to $listSessions and all members of the deployment. Until the session record exists in the system.sessions collection, you can only list the session via the $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 in the pipeline.

The stage has the following syntax:

{ $listLocalSessions: <document> }

The $listLocalSessions stage takes a document with one of the following contents:

Field
Description

{ }

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 listSessions action 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 listSessions action on the cluster.

$listLocalSessions is not allowed in transactions.

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 listSessions action on the cluster.

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

From the connected mongod or 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 listSessions action on the cluster.

db.aggregate( [ { $listLocalSessions: { users: [ { user: "myAppReader", db: "test" } ] } } ] )

From the connected mongod / mongos instance's in-memory cache, the following aggregation operation lists all sessions for the current user if run with access control:

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

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

To use the MongoDB Node.js driver to add a $listLocalSessions stage to an aggregation pipeline, use the $listLocalSessions operator in a pipeline object.

The following aggregation operation lists all local sessions:

const pipeline = [{ $listLocalSessions: { allUsers: true } }];
const cursor = db.aggregate(pipeline);
return cursor;

Note

If running with access control, the current user must have privileges with listSessions action on the cluster.

The following aggregation operation lists all the sessions for the specified user myAppReader@test:

const pipeline = [
{
$listLocalSessions: {
users: [{ user: "myAppReader", db: "test"}]
}
}
];
const cursor = db.aggregate(pipeline);
return cursor;

Note

If running with access control and the current user is not the specified user, the current user must have privileges with listSessions action on the cluster.

If run with access control, the following aggregation operation lists all sessions for the current user:

const pipeline = [{ $listLocalSessions: {} }];
const cursor = db.aggregate(pipeline);
return cursor;

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

Back

$listClusterCatalog

On this page