Definition定义
$listLocalSessionsLists the sessions cached in memory by the列出mongodormongosinstance.mongod或mongos实例在内存中缓存的会话。Important
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.mongod或mongos实例上创建会话时,会话的记录最初只存在于实例的内存中;即,记录是实例的本地记录。Periodically, the instance will sync its cached sessions to the实例会定期将其缓存的会话同步到system.sessionscollection in theconfigdatabase, at which time, they are visible to$listSessionsand all members of the deployment.config数据库中的system.sessions集合,此时,$listSessions和部署的所有成员都可以看到这些会话。Until the session record exists in the在system.sessionscollection, you can only list the session via the$listLocalSessionsoperation.system.sessions集合中存在会话记录之前,您只能通过$listLocalSessions操作列出会话。The$listLocalSessionsoperation uses thedb.aggregate()method and not thedb.collection.aggregate().$listLocalSessions操作使用db.aggregate()方法,而不是db.collection.aggregate()。To run要运行$listLocalSessions, it must be the first stage in the pipeline.$listLocalSessions,它必须是管道中的第一阶段。The stage has the following syntax:该阶段具有以下语法:{ $listLocalSessions: <document> }The$listLocalSessionsstage takes a document with one of the following contents:$listLocalSessions阶段接收具有以下内容之一的文档: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返回指定用户的所有会话。如果使用访问控制运行,则经过身份验证的用户必须具有群集上listSessionsaction on the cluster to list sessions for other users.listSessions操作的权限,才能为其他用户列出会话。{ allUsers: true }Returns all sessions for all users. If running with access control, the authenticated user must have privileges with返回所有用户的所有会话。如果使用访问控制运行,则经过身份验证的用户必须具有群集上listSessionsaction on the cluster.listSessions操作的权限。
Restrictions限制
事务中不允许使用$listLocalSessions is not allowed in transactions.$listLocalSessions。
Examples示例
MongoDB Shell
List All Local Sessions列出所有本地会话
From the connected 从连接的mongod / mongos instance's in-memory cache of sessions, the following aggregation operation lists all sessions:mongod/mongos实例的会话内存缓存中,以下聚合操作列出了所有会话:
Note
If running with access control, the current user must have privileges with 如果使用访问控制运行,则当前用户必须具有群集上listSessions action on the cluster.listSessions操作的权限。
db.aggregate( [ { $listLocalSessions: { allUsers: true } } ] )List All Local Sessions for the Specified Users列出指定用户的所有本地会话
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:mongod或mongos实例的内存缓存中,以下聚合操作列出了指定用户的所有会话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.listSessions操作的权限。
db.aggregate( [ { $listLocalSessions: { users: [ { user: "myAppReader", db: "test" } ] } } ] )List All Local Sessions for the Current User列出当前用户的所有本地会话
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:mongod/mongos实例的内存缓存中列出当前用户的所有会话:
db.aggregate( [ { $listLocalSessions: { } } ] )
If run without access control, the operation lists all local sessions.如果在没有访问控制的情况下运行,该操作将列出所有本地会话。
Node.js
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序将$listLocalSessions stage to an aggregation pipeline, use the $listLocalSessions operator in a pipeline object.$listLocalSessions阶段添加到聚合管道中,请在管道对象中使用$listLocalSession运算符。
List All Local Sessions列出所有本地会话
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.listSessions操作的权限。
List All Local Sessions for the Specified Users列出指定用户的所有本地会话
The following aggregation operation lists all the sessions for the specified user 以下聚合操作列出了指定用户的所有会话myAppReader@test: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.listSessions操作的权限。
List All Local Sessions for the Current User列出当前用户的所有本地会话
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.如果在没有访问控制的情况下运行,该操作将列出所有本地会话。