Database Manual / Reference / Database Commands / Diagnostics

dbHash (database command数据库命令)

Definition定义

dbHash
Returns the hash values of the collections in a database and an MD5 value for these collections. 返回数据库中集合的哈希值以及这些集合的MD5值。dbHash is useful to compare databases across mongod instances, such as across members of replica sets.dbHash对于跨mongod实例(例如跨副本集的成员)比较数据库非常有用。

Warning

The dbHash command obtains a shared (S) lock on the database, which prevents writes until the command completes.dbHash命令获取数据库上的共享(S)锁,在命令完成之前阻止写入。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.M0和Flex集群不支持此命令。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
dbHash: 1,
collections: [ <collection1>, ... ]
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Field字段Type类型Description描述
dbHashAny type任何类型The command to run. Specify any value.要运行的命令。指定任何值。
collectionsarray数组

Optional. 可选。An array of collection names.集合名称数组。

Either specify the collections for which to return the hash values, or omit or specify an empty array to return the hash values for all collections in the database.指定要返回哈希值的集合,或者省略或指定一个空数组来返回数据库中所有集合的哈希值。

Behavior行为

Non-Existent Collection不存在集合

If a collection in the collections array is non-existent, dbHash does not return a hash value for that collection.如果collections数组中的集合不存在,dbHash不会返回该集合的哈希值。

Restrictions限制

The dbHash command no longer support afterClusterTime. dbHash命令不再支持afterClusterTimeAs such, dbHash cannot be associated with causally consistent sessions.因此,dbHash不能与因果一致的会话相关联。

Return Document返回文档

The command returns a document with the following fields:该命令返回一个包含以下字段的文档:

Field字段Description描述
hostThe host and port of the mongod instance on which the command is run.运行命令的mongod实例的主机和端口。
collections

A document with the collections and their corresponding hash values.包含集合及其对应哈希值的文档。

{
<collection1>: <hash1>,
<collection2>: <hash2>,
...
}
cappedAn array that lists the capped collections.列出受限制集合的数组。
uuids

A document with the collections and their corresponding UUID values.包含集合及其对应UUID值的文档。

{
<collection1>: <UUID1>,
<collection2>: <UUID2>,
...
}
md5The aggregate hash value for these collections.这些集合的聚合哈希值。
timeMillisNumber of milliseconds to generate the hash.生成哈希的毫秒数。
ok
operationTime
$clusterTime
Returned with every command. 随每个命令返回。See also Response for details.详见响应

Examples示例

Return Hash Values for All Collections in a Database返回数据库中所有集合的哈希值

The following example returns the hash value for all collections in the database test:以下示例返回数据库test中所有集合的哈希值:

use test
db.runCommand( { dbHash: 1 } )

The operation returns the following document:该操作返回以下文档:

{
"host" : "myHostName.local:27017",
"collections" : {
"foo" : "d27b769230edc551d869060ec3fb68bd",
"inventory" : "ec3d821581ea1bd3aa8196c94b946874",
"log" : "d41d8cd98f00b204e9800998ecf8427e",
"orders" : "0242c0a128c284ea9576a34db2306c12",
"restaurants" : "5dc9b88091c36f0d529567b5b6e3fc92",
"zipcodes" : "31ede812bf397509a87359c65bf2a08c"
},
"capped" : [
"log"
],
"uuids" : {
"foo" : UUID("469592fe-3bfe-425e-975f-cedbe0c4741d"),
"inventory" : UUID("0830e0ad-cc24-4fc7-80d0-8e22fe45e382"),
"log" : UUID("4be024ff-711b-4ab8-836b-dee662e090f1"),
"orders" : UUID("755be489-745f-400c-ac3b-f27525ad0108"),
"restaurants" : UUID("520b56ec-3276-4904-b6e5-286bc9bfa648"),
"zipcodes" : UUID("12e97b70-c174-40af-a178-5d83a241fe20")
},
"md5" : "0cb7417ae9d9eb865000b4debdc671da",
"timeMillis" : 53,
"ok" : 1,
"operationTime" : Timestamp(1529208582, 4),
"$clusterTime" : {
"clusterTime" : Timestamp(1529208582, 4),
"signature" : {
"hash" : BinData(0,"X3MmevDqUgCVvN1AhnT+fiOL/Lc="),
"keyId" : Long("6567898567824900097")
}
}
}

Return Hash Values for Specified Collections in a Database返回数据库中指定集合的哈希值

The following example returns the hash value for the collections inventory and orders in the database test:以下示例返回数据库test中集合inventoryorders的哈希值:

use test
db.runCommand( { dbHash: 1, collections: [ "inventory", "orders" ] } )

The operation returns the following document:该操作返回以下文档:

{
"host" : "myHostName.local:27017",
"collections" : {
"inventory" : "ec3d821581ea1bd3aa8196c94b946874",
"orders" : "0242c0a128c284ea9576a34db2306c12"
},
"capped" : [ ],
"uuids" : {
"inventory" : UUID("0830e0ad-cc24-4fc7-80d0-8e22fe45e382"),
"orders" : UUID("755be489-745f-400c-ac3b-f27525ad0108")
},
"md5" : "cb4676f316ff2ff29c701a5edd18afe3",
"timeMillis" : 0,
"ok" : 1,
"operationTime" : Timestamp(1529208801, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1529208801, 1),
"signature" : {
"hash" : BinData(0,"I4z4a4Mgs+tcx0MP5xIU8DYAMB0="),
"keyId" : Long("6567898567824900097")
}
}
}