定义
$listLocalSessions重要
When a user creates a session on a
mongodormongosinstance, 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 thesystem.sessionscollection in theconfigdatabase, at which time, they are visible to$listSessionsand all members of the deployment. Until the session record exists in thesystem.sessionscollection, you can only list the session via the$listLocalSessionsoperation.The
$listLocalSessionsoperation uses thedb.aggregate()method and not thedb.collection.aggregate().To run
$listLocalSessions, it must be the first stage in the pipeline.该阶段采用以下语法:
{ $listLocalSessions: <document> } The
$listLocalSessionsstage takes a document with one of the following contents:字段说明{ }如果使用访问控制运行,则返回当前经过身份验证的用户的所有会话。
如果在没有访问控制的情况下运行,则返回所有会话。
{ users: [ { user: <user>, db: <db> }, ... ] }返回指定用户的所有会话。 如果使用访问控制运行,则经过身份验证的用户必须具有对集群执行
listSessions操作的权限,才能列出其他用户的会话。{ allUsers: true }返回所有用户的所有会话。 如果使用访问控制运行,则经过身份验证的用户必须具有对集群执行
listSessions操作的权限。
限制
$listLocalSessions 不允许在事务中使用。
示例
列出所有本地会话
从连接的mongod / mongos实例的会话内存缓存中,以下聚合操作会列出所有会话:
注意
如果使用访问控制运行,则当前用户必须具有对集群执行listSessions操作的权限。
db.aggregate( [ { $listLocalSessions: { allUsers: true } } ] )
列出指定用户的所有本地会话
从连接的 mongod 或 mongos 实例的内存缓存,以下聚合操作列出指定用户 myAppReader@test 的所有会话:
注意
如果使用访问控制运行且当前用户不是指定用户,则当前用户必须具有对集群执行listSessions操作的权限。
db.aggregate( [ { $listLocalSessions: { users: [ { user: "myAppReader", db: "test" } ] } } ] )
列出当前用户的所有本地会话
在已连接的mongod / mongos实例的内存缓存中,以下聚合操作会列出当前用户的所有会话(如果使用访问控制来运行):
db.aggregate( [ { $listLocalSessions: { } } ] )
如果在没有访问控制的情况下运行,该操作会列出所有本地会话。
要使用MongoDB Node.js驱动程序将 $listLocalSessions 阶段添加到聚合管道,请在管道对象中使用 $listLocalSessions操作符。
列出所有本地会话
以下聚合操作列出了所有本地会话:
const pipeline = [{ $listLocalSessions: { allUsers: true } }]; const cursor = db.aggregate(pipeline); return cursor;
注意
如果使用访问控制运行,则当前用户必须具有对集群执行listSessions操作的权限。
列出指定用户的所有本地会话
以下聚合操作列出指定用户 myAppReader@test 的所有会话:
const pipeline = [ { $listLocalSessions: { users: [{ user: "myAppReader", db: "test"}] } } ]; const cursor = db.aggregate(pipeline); return cursor;
注意
如果使用访问控制运行且当前用户不是指定用户,则当前用户必须具有对集群执行listSessions操作的权限。
列出当前用户的所有本地会话
如果使用访问权限控制运行,则以下聚合操作会列出当前用户的所有会话:
const pipeline = [{ $listLocalSessions: {} }]; const cursor = db.aggregate(pipeline); return cursor;
如果在没有访问权限控制的情况运行,该操作会列出所有本地会话。
输出
$listLocalSessions 返回一个游标,其中每个文档在内存中缓存一个会话。每个文档都有以下原型:
{ _id: { id: <UUID>, uid: <BinData> }, lastUse: <date> }
字段 | 类型 | 说明 |
|---|---|---|
| UUID | 会话的全局唯一标识符 (UUID)。 |
| BinData | 与会话关联的经过身份验证的用户凭证的哈希值。 |
| 日期 | 上次使用会话的日期和时间。 |