MongoDB資料比較工具dbHash介紹

chenfeng發表於2016-05-13
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: [ <collection1>, ... ] } )

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2099041/,如需轉載,請註明出處,否則將追究法律責任。

相關文章