Database Manual / Reference / Query Language / Aggregation Stages

$listSampledQueries (aggregation stage)(聚合阶段)

Definition定义

$listSampledQueries
Returns sampled queries for all collections or a specific collection. Sampled queries are used by the analyzeShardKey command to calculate metrics about the read and write distribution of a shard key.返回所有集合或特定集合的采样查询。analyzeShardKey命令使用采样查询来计算分片键的读写分布指标。

Syntax语法

$listSampledQueries has this syntax:具有以下语法:

{
$listSampledQueries: { namespace: <namespace> }
}

Behavior行为

  • To list sampled queries for a single collection, specify the collection in the namespace argument.要列出单个集合的采样查询,请在namespace参数中指定该集合。
  • To list sampled queries for all collections, omit the namespace argument.要列出所有集合的采样查询,请省略namespace参数。

Access Control访问控制

$listSampledQueries requires the clusterMonitor role on the cluster.需要群集上的clusterMonitor角色。

Limitations局限性

  • You cannot use $listSampledQueries on Atlas multi-tenant configurations.您不能在Atlas多租户配置上使用$listSampledQueries
  • You cannot use $listSampledQueries on standalone deployments.您不能在单机版部署上使用$listSampledQueries
  • You cannot use $listSampledQueries directly against a --shardsvr replica set. 您不能直接对--shardsvr副本集使用$listSampledQueriesWhen running on a sharded cluster, $listSampledQueries must run against a mongos.在分片集群上运行时,$listSampledQueries必须对mongos运行。

Examples示例

MongoDB Shell

List Sampled Queries for All Collections列出所有集合的采样查询

The following aggregation operation lists all sampled queries for all collections in the replica set:以下聚合操作列出了副本集中所有集合的所有采样查询:

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

List Sampled Queries for A Specific Collection列出特定集合的采样查询

The following aggregation operation lists all sampled queries for a post collection in the social database:以下聚合操作列出了social(社交)数据库中post集合的所有采样查询:

db.aggregate( [  { $listSampledQueries: { namespace: "social.post" } } ] )
Node.js

To use the MongoDB Node.js driver to add a $listSampledQueries stage to an aggregation pipeline, use the $listSampledQueries operator in a pipeline object.要使用MongoDB Node.js驱动程序将$listSampledQueries阶段添加到聚合管道中,请在管道对象中使用$listSampledGues运算符。

List Sampled Queries for All Collections列出所有集合的采样查询

The following aggregation operation lists all sampled queries for all collections in the replica set:以下聚合操作列出了副本集中所有集合的所有采样查询:

const pipeline = [{ $listSampledQueries: {} }];

const cursor = db.aggregate(pipeline);
return cursor;

List Sampled Queries for A Specific Collection列出特定集合的采样查询

The following aggregation operation lists all sampled queries for the movies collection in the sample_mflix database:以下聚合操作列出了sample_mflix数据库中movies集合的所有采样查询:

const pipeline = [{ $listSampledQueries: { namespace: "sample_mflix.movies" } }];

const cursor = db.aggregate(pipeline);
return cursor;

Output输出

The output fields differ for read and write queries.读和写查询的输出字段不同。

Read Queries读取查询

{
_id: <uuid>,
ns: "<database>.<collection>",
collectionUuid: <collUUID>,
cmdName: <find|aggregate|count|distinct>,
cmd: {
filter: <object>,
collation: <object>,
let: <object>
},
expireAt: <date>
}
Field Name字段名称Type类型Description描述
_idUUIDSample ID for the query.查询的样本ID。
nsstring字符串Namespace of the sampled collection.
collectionUuidUUIDID of the sampled collection.采样集合的ID。
cmdNamestring字符串

Name of the sampled command. Can be one of:采样命令的名称。可以是以下之一:

  • "find"
  • "aggregate"
  • "count"
  • "distinct"
cmd.filterobjectFilter the command ran with, if applicable.筛选与一起运行的命令(如果适用)。
cmd.collationobjectCollation the command ran with, if applicable.如果适用,请对运行的命令进行排序。
cmd.letobjectCustom variables the command ran with, if applicable.命令运行时使用的自定义变量(如果适用)。
expireAtdateDate that the sample expires.样本过期的日期。

Write Queries写入查询

{
_id: <uuid>,
ns: "<database>.<collection>",
collectionUuid: <collUUID>,
cmdName: <update|delete|findAndModify>,
cmd: <object>,
expireAt: <date>
}
Field Name字段名称Type类型Description描述
_idUUIDSample ID for the query.查询的样本ID。
nsstring字符串Namespace of the sampled collection.采样集合的命名空间。
collectionUuidUUIDID of the sampled collection.采样集合的ID。
cmdNamestring字符串

Name of the sampled command. Can be one of:采样命令的名称。可以是以下之一:

  • "update"
  • "delete"
  • "findAndModify"
cmdobjectCommand object命令对象
expireAtdateDate that the sample expires.样本过期的日期。