AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

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.

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

このコマンドの構文は、次のとおりです。

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

このコマンドは、次のフィールドを使用します。

フィールド
タイプ
説明

dbHash

任意の型

実行するコマンド。 任意の値を指定します。

collections

配列

任意。現在のデータベース内のコレクション名の配列。各コレクションを名前のみで指定します。test.orders のように、データベース名で名前を修飾しないでください。

データベース内のすべてのコレクションのハッシュされた値を返すには、空の配列を指定します。

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

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