Skip to content

Instantly share code, notes, and snippets.

@nitredd
Created August 23, 2025 17:39
Show Gist options
  • Select an option

  • Save nitredd/6969ab6cd7902f9a7b866c7780b099ed to your computer and use it in GitHub Desktop.

Select an option

Save nitredd/6969ab6cd7902f9a7b866c7780b099ed to your computer and use it in GitHub Desktop.
Verification after data migration between MongoDB clusters
/*
Author: Katkam Nitin Reddy < redknitin [at] gmail [dot] com >
Date: September 26, 2022
Description: This script uses the dbHash command to compare the source and destination clusters
Using the Mongo shell, connect to the source cluster and run this script
This script needs write access to the target cluster to write the log
*/
//Commented this out - We are no longer writing to a log; console output only
//verificationDbName = 'verifyLog'
otherMongoUri = 'mongodb://username_here:[email protected]:27017,cluster0-shard-00-01.fdu3e.mongodb.net:27017,cluster0-shard-00-02.fdu3e.mongodb.net:27017/?tls=true&replicaSet=atlas-jusd5q-shard-0' //this is the target's URI
otherMongo = new Mongo(otherMongoUri)
dbList = db.adminCommand({listDatabases: 1}).databases.map(i => i.name)
for (i=0; i<dbList.length; i++) {
//Skipping system DBs
if (dbList[i]=="admin" || dbList[i]=="config" || dbList[i]=="local") continue;
collList = db.getSiblingDB(dbList[i]).getCollectionNames()
hashes = db.getSiblingDB(dbList[i]).runCommand({dbHash: 1}).collections
otherHashes = otherMongo.getDB(dbList[i]).runCommand({dbHash: 1}).collections
for (j=0; j<collList.length; j++) {
isMatched = false
if (hashes[collList[j]] == otherHashes[collList[j]]) {
print("YES: " + dbList[i] + "-" + collList[j])
isMatched = true
} else {
print("NO: " + dbList[i] + "-" + collList[j])
}
//Commented this out to prevent writes
//otherMongo.getDB(verificationDbName).hashMatch.insertOne({_id: {db: dbList[i], coll: collList[j]}, matched: isMatched})
}
}
//Commented this out - We are no longer writing to a log; console output only
//List mismatched collections
//otherMongo.getDB(verificationDbName).hashMatch.find({matched: false, "_id.db": {$nin: ["admin", "local", "config"]}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment