$listSessions is a MongoDB aggregation stage that lists logical sessions across a sharded cluster when run on mongos—a single cluster-wide inventory instead of checking each shard with $listLocalSessions.
Remember
$listSessions = "who has sessions across the CLUSTER?"
db.getSiblingDB("admin").aggregate([
{ $listSessions: {} }
])
connect via mongos (sharded cluster)
must run on admin
must be the FIRST stage
allUsers: false → your sessions only (default)
allUsers: true → every session in cluster scope
lastUse → when the session was last active
$this node only → $listLocalSessions
cluster view → $listSessions
assert: connected to mongos (sharded cluster)
assert: pipeline runs on admin
assert: $listSessions is stage 1
sessions = logical sessions visible cluster-wide
if allUsers is false:
keep only sessions for current user
else:
keep all sessions in cluster scope
(requires privilege when auth is on)
for each session:
emit { id, user, lastUse, … }
// later $match / $sort / $project reshape the list
Reference
Cheat Sheet
Keep these facts handy during cluster session audits.
Topic
Detail
Type
Aggregation stage (admin diagnostics)
Target
Prefer mongos on a sharded cluster
Database
Must run on admin
Position
Must be first
Default scope
Caller’s sessions only
Admin scope
allUsers: true
Privilege
Often listSessions for all users
vs $listLocalSessions
Cluster-oriented vs this node only
Try it
Live Preview
Filter a sample cluster session snapshot (teaching model—not a live mongos connection).
Sample assumes you are logged in as appUser on mongos. Timestamps are relative to “now.”
Result
appUser @ admin lastUse: 2 min ago
appUser @ admin lastUse: 40 min ago
Trace
Worked Walkthrough — List Your Cluster Sessions
You are connected to mongos with mongosh. Goal: see your logical sessions across the cluster, newest activity first.
Calculation
1. Connect to mongos
2. db.getSiblingDB("admin")
3. $listSessions {} ← first stage
4. Defaults: allUsers false → only your sessions
5. $project id, user, lastUse
6. Optional: $sort { lastUse: -1 }
Any find()/update you run updates lastUse
on your client session record.
Code
MongoDB Examples
These pipelines run on the admin database via mongos and inspect live session metadata (not a seeded collection). Results depend on who is connected to your cluster.
Example 1 — Setup / Prerequisites
Goal: connect to mongos, touch your client session, then list logical sessions cluster-wide. $listSessions reads server session metadata on admin—not documents from a collection you seed.
mongosh
// Touch a session (any database is fine)
db.getSiblingDB("test").products.findOne()
// List sessions — must use admin on mongos
db.getSiblingDB("admin").aggregate([
{ $listSessions: {} }
])
Output (sample shape)
{
id: UUID("..."),
lastUse: ISODate("..."),
user: { user: "myApp", db: "admin" }
}
// Exact fields/values vary by MongoDB version and auth setup
How It Works
Session exists. Any command (here findOne) ties work to your logical session.
Admin only. Switch with getSiblingDB("admin") before aggregating.
Stage 1.$listSessions must be first; default allUsers: false shows your sessions.
Output shape. Each result is session info (id, user, lastUse), not business data.
Example 2 — Clean Fields
Goal: after Example 1, keep only the fields you need for a quick audit of your cluster sessions.
One line:$listSessions answers “which logical sessions exist across this sharded cluster right now?”
Frequently Asked Questions
$listSessions returns documents describing logical sessions tracked across a sharded cluster when you run it on mongos. Each document includes session metadata such as id, user, and lastUse—broader than $listLocalSessions on a single node.
Connect to mongos and run on admin: db.getSiblingDB("admin").aggregate([{ $listSessions: {} }]). The stage must be first. Prefer $listLocalSessions for single-node or replica-set checks.
By default (allUsers: false), you see only sessions for the user running the command. Set allUsers: true to list every session in cluster scope—requires listSessions privilege when access control is enabled.
$listLocalSessions lists sessions on the connected mongod/mongos only. $listSessions on mongos aggregates session records across the sharded cluster. Use local for one-node diagnostics; use listSessions for cluster-wide audits.
Yes. $listSessions must be stage 1, but you can chain $match, $sort, $project, $group, and $limit afterward—for example filter by lastUse or count sessions per user.
$listSessions inventories session records. $currentOp shows live and idle operations (often with lsid). Use listSessions for session counts and staleness; use currentOp to see what is running now.