$listLocalSessions is a MongoDB aggregation stage that lists logical sessions on the current mongod (or mongos)—server-side records used for retryable writes, transactions, and causal consistency.
Remember
$listLocalSessions = "who has sessions on THIS node?"
db.getSiblingDB("admin").aggregate([
{ $listLocalSessions: {} }
])
must run on admin
must be the FIRST stage
allUsers: false → your sessions only (default)
allUsers: true → every session on this node
lastUse → when the session was last active
$this node only → not cluster-wide
cluster view → $listSessions
assert: pipeline runs on admin
assert: $listLocalSessions is stage 1
sessions = logical sessions cached on THIS node
if allUsers is false:
keep only sessions for current user
else:
keep all sessions on this node
(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 session audits.
Topic
Detail
Type
Aggregation stage (admin diagnostics)
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 $listSessions
This node vs cluster-oriented view
Try it
Live Preview
Filter a sample session snapshot (teaching model—not a live mongod connection).
Sample assumes you are logged in as appUser. Timestamps are relative to “now.”
Result
appUser @ admin lastUse: 2 min ago
appUser @ admin lastUse: 40 min ago
Trace
Worked Walkthrough — List Your Sessions
You are connected with mongosh. Goal: see your logical sessions on this node, newest activity first.
Calculation
1. db.getSiblingDB("admin")
2. $listLocalSessions {} ← first stage
3. Defaults: allUsers false → only your sessions
4. $project id, user, lastUse
5. 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 and inspect live session metadata (not a seeded collection). Results depend on who is connected to your server.
Example 1 — Setup / Prerequisites
Goal: touch your client session, then list logical sessions on this node. $listLocalSessions 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
db.getSiblingDB("admin").aggregate([
{ $listLocalSessions: {} }
])
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) updates your session’s lastUse.
Admin only. Switch with getSiblingDB("admin") before aggregating.
Stage 1.$listLocalSessions must be first; default scope is your sessions only.
This node. Results are local to the mongod/mongos you are connected to—not cluster-wide.
Example 2 — Clean Fields
Goal: after Example 1, keep only the fields you need for a quick audit of your sessions.
One line:$listLocalSessions answers “which logical sessions exist on this server right now?”
Frequently Asked Questions
$listLocalSessions returns documents describing logical sessions recorded on the current mongod (or mongos) you are connected to—not cluster-wide. Each document includes session metadata such as id, user, and lastUse.
Run on admin: db.getSiblingDB("admin").aggregate([{ $listLocalSessions: {} }]). The stage must be first. It does not run on regular application collections.
By default (allUsers: false), you see only sessions for the user running the command. Set allUsers: true to list every session on this node—requires listSessions privilege when access control is enabled.
$listLocalSessions is scoped to the connected mongod/mongos only. $listSessions (typically on mongos) can return session information across a sharded cluster. Use local for one-node diagnostics.
Yes. $listLocalSessions must be stage 1, but you can chain $match, $sort, $project, and $limit afterward—for example filter by lastUse or project only id and user.
$currentOp shows operations (including idle sessions). $listLocalSessions focuses on logical session records themselves. Use both when debugging transactions or connection-pool session load.