Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1. Configure settings below
- // 2. Run with "mongo db-spotchecks.js"
- uriOld = "mongodb://user:pass@hosname:27017/admin?replicaSet=MySet&ssl=true"
- uriNew = "mongodb://localhost"
- dbName = "MyDB";
- excludeCollections= ["CronLog", "HitLog", "Freq", "SystemLog", "Message"] // Exclude any non-critical collections
- sample = {
- min: 10,
- max: 2000
- };
- // connect to mongos
- connOld = new Mongo(uriOld);
- dbOld = connOld.getDB(dbName);
- connNew = new Mongo(uriNew);
- dbNew = connNew.getDB(dbName);
- var collPattern = new RegExp("^(" + excludeCollections.join("|") + "fs|system)\.");
- print("regexp=" + collPattern.toString());
- // For each collection, pick 10 docs at random and compare
- dbOld.getCollectionNames().forEach(function(collname) {
- if (!collPattern.exec(collname)) {
- // Check count and size first
- cstatsOld = dbOld.runCommand( { collStats : collname } )
- cstatsNew = dbNew.runCommand( { collStats : collname } )
- // printjson(cstatsOld);
- // printjson(cstatsNew);
- if (cstatsOld.count === cstatsNew.count) {
- print("Count " + cstatsOld.count + " OK");
- } else {
- print("ERR Count mismatch " + cstatsOld.count + " -> " + cstatsNew.count) ;
- }
- if (cstatsOld.size === cstatsNew.size) {
- print("Size " + cstatsOld.size + " OK");
- } else {
- print("ERR Size mismatch " + cstatsOld.size + " -> " + cstatsNew.size) ;
- }
- fails = 0;
- // sample between 10 and 2000 docs
- count = dbOld[collname].count();
- sampleSize = Math.floor(Math.min(sample.max, Math.max(count * 0.001, sample.min)));
- print(collname + " count="+ count +" with sampleSize="+sampleSize);
- dbOld[collname].aggregate(
- [{
- $sample: {
- size: sampleSize
- }
- }]
- ).forEach(function(docOld) {
- docNew = dbNew[collname].findOne({
- _id: docOld._id
- });
- stringOld = JSON.stringify(docOld);
- stringNew = JSON.stringify(docNew);
- if (stringOld == stringNew) {
- // print("Doc matches: " + docOld._id);
- } else {
- print("Doc mismatch: " + docOld._id);
- print("Old=");
- printjson(docOld);
- print("New=");
- printjson(docNew);
- fails++;
- }
- });
- if (fails > 0) {
- print(fails + " fails");
- } else {
- print("All OK");
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement