对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

dbHash(数据库命令)

dbHash

Returns the hash values of the collections in a database and an MD5 value for these collections. dbHash is useful to compare databases across mongod instances, such as across members of replica sets.

警告

The dbHash command obtains a shared (S) lock on the database, which prevents writes until the command completes.

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud

重要

This command is not supported in M0 and Flex clusters. For more information, see Unsupported Commands.

该命令具有以下语法:

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

该命令接受以下字段:

字段
类型
说明

dbHash

任何类型

要运行的命令。可以指定任何值。

collections

阵列

可选。集合名称的数组。

指定要返回哈希值的集合,或者省略或指定一个空数组以返回数据库中所有集合的哈希值。

maxTimeMS

non-negative integer

可选。

指定时间限制(以毫秒为单位)。如果您未指定 maxTimeMS 值,操作将不会超时。如果值为 0 ,则显式指定默认无限制行为。

MongoDB 使用与 db.killOp() 相同的机制终止超过分配的时间限制的操作。MongoDB 仅在指定的中断点之一中终止操作。

If a collection in the collections array is non-existent, dbHash does not return a hash value for that collection. A database-qualified name such as test.orders never matches, because dbHash compares each entry against the collection names in the current database.

如果 collections 数组中没有名称与集合匹配,或者集合存在但为空,则 dbHash 返回空输入的 md5 值,即 d41d8cd98f00b204e9800998ecf8427e。

注意

dbhash 的输出不区分不存在的集合和空集合。

在跨部署比较 md5 值之前,请确认返回文档的 collections 字段列出了您期望的每个集合。

The dbHash command no longer support afterClusterTime. As such, dbHash cannot be associated with causally consistent sessions.

此命令将返回一个包含以下字段的文档:

字段
说明

主机

运行命令的 mongod 实例的主机和端口。

collections

包含集合及其相应哈希值的文档。

{
<collection1>: <hash1>,
<collection2>: <hash2>,
...
}

capped

列出固定大小集合的数组。

uuids

包含集合及其相应 UUID 值的文档。

{
<collection1>: <UUID1>,
<collection2>: <UUID2>,
...
}

md5

这些集合的聚合哈希值。

timeMillis

生成哈希值所需的毫秒数。

ok
operationTime
$clusterTime

每个命令都会返回。详情另见响应。

以下示例返回数据库 test 中所有集合的哈希值:

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

该操作将返回以下文档:

{
"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")
}
}
}

以下示例返回数据库 test 中集合 inventory 和 orders 的哈希值:

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

该操作将返回以下文档:

{
"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")
}
}
}