Definition定义
$shardedDataDistributionNew in version 6.0.3.在版本6.0.3中新增。Returns information on the distribution of data in sharded collections.返回分片集合中数据分布的信息。Note
This aggregation stage is only available on此聚合阶段仅适用于mongos.mongos。This aggregation stage must be run on the此聚合阶段必须在admindatabase. The user must have theshardedDataDistributionprivilege action.admin数据库上运行。用户必须具有shardedDataDistribution权限操作。
Syntax语法
The shardedDataDistribution stage has the following syntax:shardedDataDistribution阶段具有以下语法:
db.aggregate( [
{ $shardedDataDistribution: { } }
] )Output Fields输出字段
The $shardedDataDistribution stage outputs an array of documents for each sharded collection in the database. These documents contain the following fields:$shardedDataDistribution阶段为数据库中的每个分片集合输出一组文档。这些文档包含以下字段:
ns | ||
shards | ||
shards.numOrphanedDocs |
| |
shards.numOwnedDocuments |
| |
shards.ownedSizeBytes | ||
shards.orphanedSizeBytes |
Starting in MongoDB 8.0, 从MongoDB 8.0开始,如果主分片有块或孤立文档,$shardedDataDistribution only returns output for a collection's primary shard if the primary shard has chunks or orphaned documents.$sharededDataDistribution只会返回集合primary分片的输出。
Behavior行为
After an unclean shutdown of a 在使用Wired Tiger存储引擎不干净地关闭mongod using the Wired Tiger storage engine, size and count statistics reported by $shardedDataDistribution may be inaccurate.mongod后,$shardedDataDistribution报告的大小和计数统计数据可能不准确。
The amount of drift depends on the number of insert, update, or delete operations performed between the last checkpoint and the unclean shutdown. 漂移量取决于在最后一个检查点和不干净关闭之间执行的插入、更新或删除操作的数量。Checkpoints usually occur every 60 seconds. However, 检查点通常每60秒出现一次。然而,使用非默认的mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.--syncdelay设置运行的mongod实例可能或多或少地具有检查点。
Run 在validate on each collection on the mongod to restore statistics after an unclean shutdown.mongod上的每个集合上运行validate,以在不干净关闭后恢复统计数据。
After an unclean shutdown:不干净停机后:
Examples示例
MongoDB Shell
Return All Sharded Data Distibution Metrics返回所有分片数据分布指标
To return all sharded data distribution metrics, run the following:要返回所有分片数据分布指标,请运行以下命令:
db.aggregate([
{ $shardedDataDistribution: { } }
])
Example output:输出示例:
[
{
"ns": "test.names",
"shards": [
{
"shardName": "shard-1",
"numOrphanedDocs": 0,
"numOwnedDocuments": 6,
"ownedSizeBytes": 366,
"orphanedSizeBytes": 0
},
{
"shardName": "shard-2",
"numOrphanedDocs": 0,
"numOwnedDocuments": 6,
"ownedSizeBytes": 366,
"orphanedSizeBytes": 0
}
]
}
]Return Metrics for a Specific Shard特定分片的回报指标
To return sharded data distribution metrics for a specific shard, run the following:要返回特定分片的分片数据分布指标,请运行以下命令:
db.aggregate([
{ $shardedDataDistribution: { } },
{ $match: { "shards.shardName": "<name of the shard>" } }
])Return Metrics for a Namespace命名空间的返回度量
To return sharded data distribution data for a namespace, run the following:要返回命名空间的分片数据分发数据,请运行以下命令:
db.aggregate([
{ $shardedDataDistribution: { } },
{ $match: { "ns": "<database>.<collection>" } }
])Confirm No Orphaned Documents Remain确认无遗留孤立文件
Starting in MongoDB 6.0.3, you can run an aggregation using the 从MongoDB 6.0.3开始,您可以使用$shardedDataDistribution stage to confirm no orphaned documents remain:$sharededDataDistribution阶段运行聚合,以确认没有孤立文档残留:
db.aggregate([
{ $shardedDataDistribution: { } },
{ $match: { "ns": "<database>.<collection>" } }
])
$shardedDataDistribution has output similar to the following:输出类似于以下内容:
[
{
"ns": "test.names",
"shards": [
{
"shardName": "shard-1",
"numOrphanedDocs": 0,
"numOwnedDocuments": 6,
"ownedSizeBytes": 366,
"orphanedSizeBytes": 0
},
{
"shardName": "shard-2",
"numOrphanedDocs": 0,
"numOwnedDocuments": 6,
"ownedSizeBytes": 366,
"orphanedSizeBytes": 0
}
]
}
]
Ensure that 确保集群中每个分片的"numOrphanedDocs" is 0 for each shard in the cluster."numOrphanedDocs"为0。
Node.js
To use the MongoDB Node.js driver to add a 要使用MongoDB Node.js驱动程序将$shardedDataDistribution stage to an aggregation pipeline, use the $shardedDataDistribution operator in a pipeline object.$sharededDataDistribution阶段添加到聚合管道中,请在管道对象中使用$shardedDataDistribution运算符。
Return All Sharded Data Distribution Metrics返回所有分片数据分布指标
The following example creates a pipeline stage that returns information about the distribution of data in sharded collections. The example then runs the aggregation pipeline:以下示例创建了一个管道阶段,该阶段返回有关分片集合中数据分布的信息。然后,该示例运行聚合管道:
const pipeline = [{$shardedDataDistribution: {} }];
const adminDb = client.db("admin");
const cursor = adminDb.aggregate(pipeline);
return cursor;
Return Metrics for a Specific Shard特定分片的回报指标
The following example returns information about the distribution of data for a specific shard:以下示例返回特定分片的数据分布信息:
const pipeline = [
{ $shardedDataDistribution: {} },
{ $match: {"shards.shardName": 'atlas-kn29y8-shard-0'} }
];
const adminDb = client.db("admin");
const cursor = adminDb.aggregate(pipeline);
return cursor;Return Metrics for a Namespace命名空间的返回度量
The following example returns information about the distribution of data for a specific namespace:以下示例返回特定命名空间的数据分布信息:
const pipeline = [
{ $shardedDataDistribution: {} },
{ $match: {"ns": "sample_mflix.movies"} }
];
const adminDb = client.db("admin");
const cursor = adminDb.aggregate(pipeline);
return cursor;