Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // 1. Configure settings below
  2. // 2. Run with "mongo db-spotchecks.js"
  3.  
  4. uriOld = "mongodb://user:pass@hosname:27017/admin?replicaSet=MySet&ssl=true"
  5. uriNew = "mongodb://localhost"
  6. dbName = "MyDB";
  7. excludeCollections= ["CronLog", "HitLog", "Freq", "SystemLog", "Message"] // Exclude any non-critical collections
  8. sample = {
  9. min: 10,
  10. max: 2000
  11. };
  12.  
  13. // connect to mongos
  14. connOld = new Mongo(uriOld);
  15. dbOld = connOld.getDB(dbName);
  16.  
  17. connNew = new Mongo(uriNew);
  18. dbNew = connNew.getDB(dbName);
  19.  
  20. var collPattern = new RegExp("^(" + excludeCollections.join("|") + "fs|system)\.");
  21. print("regexp=" + collPattern.toString());
  22.  
  23. // For each collection, pick 10 docs at random and compare
  24. dbOld.getCollectionNames().forEach(function(collname) {
  25. if (!collPattern.exec(collname)) {
  26.  
  27. // Check count and size first
  28. cstatsOld = dbOld.runCommand( { collStats : collname } )
  29. cstatsNew = dbNew.runCommand( { collStats : collname } )
  30. // printjson(cstatsOld);
  31. // printjson(cstatsNew);
  32. if (cstatsOld.count === cstatsNew.count) {
  33. print("Count " + cstatsOld.count + " OK");
  34. } else {
  35. print("ERR Count mismatch " + cstatsOld.count + " -> " + cstatsNew.count) ;
  36. }
  37. if (cstatsOld.size === cstatsNew.size) {
  38. print("Size " + cstatsOld.size + " OK");
  39. } else {
  40. print("ERR Size mismatch " + cstatsOld.size + " -> " + cstatsNew.size) ;
  41. }
  42.  
  43. fails = 0;
  44. // sample between 10 and 2000 docs
  45. count = dbOld[collname].count();
  46. sampleSize = Math.floor(Math.min(sample.max, Math.max(count * 0.001, sample.min)));
  47. print(collname + " count="+ count +" with sampleSize="+sampleSize);
  48. dbOld[collname].aggregate(
  49. [{
  50. $sample: {
  51. size: sampleSize
  52. }
  53. }]
  54. ).forEach(function(docOld) {
  55. docNew = dbNew[collname].findOne({
  56. _id: docOld._id
  57. });
  58. stringOld = JSON.stringify(docOld);
  59. stringNew = JSON.stringify(docNew);
  60. if (stringOld == stringNew) {
  61. // print("Doc matches: " + docOld._id);
  62. } else {
  63. print("Doc mismatch: " + docOld._id);
  64. print("Old=");
  65. printjson(docOld);
  66. print("New=");
  67. printjson(docNew);
  68. fails++;
  69. }
  70. });
  71. if (fails > 0) {
  72. print(fails + " fails");
  73. } else {
  74. print("All OK");
  75. }
  76. }
  77. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement