dbHash

Definition

  • dbHash
  • Returns the hash values of the collections in a database and anMD5 value for these collections. dbHash is usefulto compare databases across mongod instances, such asacross members of replica sets.

dbHash has the following syntax:

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

FieldTypeDescriptiondbHashAny typeThe command to run. Specify any value.collectionsarrayOptional. 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 allcollections 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.

Restrictions

MongoDB drivers automatically set afterClusterTime for operations associated with causallyconsistent sessions. Starting in MongoDB 4.2, thedbHash command no longer support afterClusterTime. As such, dbHash cannot be associatdwith causally consistent sessions.

Return Document

The command returns a document with the following fields:

FieldDescription
hostThe host and port of the mongod instance on whichthe command is run.
collectionsA document with the collections and their corresponding hash values.
  1. { <collection1>: <hash1>, <collection2>: <hash2>, }
cappedAn array that lists the capped collections.New in version 4.0.
uuidsA document with the collections and their corresponding UUID values.
  1. { <collection1>: <UUID1>, <collection2>: <UUID2>, }
New in version 4.0.
md5The aggregate hash value for these collections.
timeMillisNumber of milliseconds to generate the hash.
okoperationTime$clusterTimeReturned with every command. See also Responsefor details.

Examples

Return Hash Values for All Collections in a Database

The following example returns the hash value for all collections in thedatabase test:

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

The operation returns the following document:

  1. {
  2. "host" : "myHostName.local:27017",
  3. "collections" : {
  4. "foo" : "d27b769230edc551d869060ec3fb68bd",
  5. "inventory" : "ec3d821581ea1bd3aa8196c94b946874",
  6. "log" : "d41d8cd98f00b204e9800998ecf8427e",
  7. "orders" : "0242c0a128c284ea9576a34db2306c12",
  8. "restaurants" : "5dc9b88091c36f0d529567b5b6e3fc92",
  9. "zipcodes" : "31ede812bf397509a87359c65bf2a08c"
  10. },
  11. "capped" : [
  12. "log"
  13. ],
  14. "uuids" : {
  15. "foo" : UUID("469592fe-3bfe-425e-975f-cedbe0c4741d"),
  16. "inventory" : UUID("0830e0ad-cc24-4fc7-80d0-8e22fe45e382"),
  17. "log" : UUID("4be024ff-711b-4ab8-836b-dee662e090f1"),
  18. "orders" : UUID("755be489-745f-400c-ac3b-f27525ad0108"),
  19. "restaurants" : UUID("520b56ec-3276-4904-b6e5-286bc9bfa648"),
  20. "zipcodes" : UUID("12e97b70-c174-40af-a178-5d83a241fe20")
  21. },
  22. "md5" : "0cb7417ae9d9eb865000b4debdc671da",
  23. "timeMillis" : 53,
  24. "ok" : 1,
  25. "operationTime" : Timestamp(1529208582, 4),
  26. "$clusterTime" : {
  27. "clusterTime" : Timestamp(1529208582, 4),
  28. "signature" : {
  29. "hash" : BinData(0,"X3MmevDqUgCVvN1AhnT+fiOL/Lc="),
  30. "keyId" : NumberLong("6567898567824900097")
  31. }
  32. }
  33. }

Return Hash Values for Specified Collections in a Database

The following example returns the hash value for the collections inventoryand orders in the database test:

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

The operation returns the following document:

  1. {
  2. "host" : "myHostName.local:27017",
  3. "collections" : {
  4. "inventory" : "ec3d821581ea1bd3aa8196c94b946874",
  5. "orders" : "0242c0a128c284ea9576a34db2306c12"
  6. },
  7. "capped" : [ ],
  8. "uuids" : {
  9. "inventory" : UUID("0830e0ad-cc24-4fc7-80d0-8e22fe45e382"),
  10. "orders" : UUID("755be489-745f-400c-ac3b-f27525ad0108")
  11. },
  12. "md5" : "cb4676f316ff2ff29c701a5edd18afe3",
  13. "timeMillis" : 0,
  14. "ok" : 1,
  15. "operationTime" : Timestamp(1529208801, 1),
  16. "$clusterTime" : {
  17. "clusterTime" : Timestamp(1529208801, 1),
  18. "signature" : {
  19. "hash" : BinData(0,"I4z4a4Mgs+tcx0MP5xIU8DYAMB0="),
  20. "keyId" : NumberLong("6567898567824900097")
  21. }
  22. }
  23. }