Definition定义
$listSampledQueriesReturns sampled queries for all collections or a specific collection. Sampled queries are used by the返回所有集合或特定集合的采样查询。analyzeShardKeycommand 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要列出单个集合的采样查询,请在namespaceargument.namespace参数中指定该集合。To list sampled queries for all collections, omit the要列出所有集合的采样查询,请省略namespaceargument.namespace参数。
Access Control访问控制
$listSampledQueries requires the 需要群集上的clusterMonitor role on the cluster.clusterMonitor角色。
Limitations局限性
You cannot use您不能在Atlas多租户配置上使用$listSampledQuerieson Atlas multi-tenant configurations.$listSampledQueries。You cannot use您不能在单机版部署上使用$listSampledQuerieson standalone deployments.$listSampledQueries。You cannot use您不能直接对$listSampledQueriesdirectly against a--shardsvrreplica set.--shardsvr副本集使用$listSampledQueries。When running on a sharded cluster,在分片集群上运行时,$listSampledQueriesmust run against amongos.$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 要使用MongoDB Node.js驱动程序将$listSampledQueries stage to an aggregation pipeline, use the $listSampledQueries operator in a pipeline object.$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>
}
_id | UUID | |
ns | Namespace of the sampled collection. | |
collectionUuid | UUID | |
cmdName |
| |
cmd.filter | object | |
cmd.collation | object | |
cmd.let | object | |
expireAt | date |
Write Queries写入查询
{
_id: <uuid>,
ns: "<database>.<collection>",
collectionUuid: <collUUID>,
cmdName: <update|delete|findAndModify>,
cmd: <object>,
expireAt: <date>
}
_id | UUID | |
ns | ||
collectionUuid | UUID | |
cmdName |
| |
cmd | object | |
expireAt | date |