MongoDB数据比较工具dbHash介绍

dbHash主要是用来比较MongoDB数据是否一致,例如升级版本前后的数据比较,主要是为了确认数据在升级后是完全恢复无误的。

命令用法如下:

> use test
switched to db test
对整个test数据库执行dbHash:
> db.runCommand({dbHash:1})
{
        "numCollections" : 6,
        "host" : "XCC-Duanshufeng",
        "collections" : {
                "articles" : "00ac934ceff133bb0098b1c35a75bdea",
                "books" : "bc52524239790921537157b52098fe84",
                "chenfeng" : "12ed2905e982feca3615bfae7b2ed588",
                "users" : "84b7f9fc8c2ea9fcabc2286ad22e9303"
        },
        "md5" : "6f8d391996f5d9860ddc5a075d3f7de3",
        "timeMillis" : 5548,
        "fromCache" : [ ],
        "ok" : 1
}
>

> show collections
articles
books
chenfeng
system.indexes
users

对test数据库某个集合执行dbHash:
> db.runCommand({dbHash:1,collections:["books","chenfeng"]})
{
        "numCollections" : 6,
        "host" : "XCC-Duanshufeng",
        "collections" : {
                "books" : "bc52524239790921537157b52098fe84",
                "chenfeng" : "12ed2905e982feca3615bfae7b2ed588"
        },
        "md5" : "cd9a81b96e0921007d94ddb3633d9af0",
        "timeMillis" : 825,
        "fromCache" : [ ],
        "ok" : 1
}
>



官网对该工具的解释如下:
dbHash
Returns the hash values of the collections in a database and an MD5 value for the list of collections. 
dbHash is useful to compare databases across mongod instances, such as mirrored config servers for sharded clusters or members of replica sets.


dbHash has the following syntax:

db.runCommand ( { dbHash: 1, collections: [ , ... ] } )
请使用浏览器的分享功能分享到微信等