Created
June 10, 2015 16:19
-
-
Save fipar/4e196e24450b4f5afd92 to your computer and use it in GitHub Desktop.
Find orphaned documents in mongodb (for pre 2.6 where cleanupOrphaned does not exist).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var database = "sample"; | |
| var collection = "tests"; | |
| var connections = []; | |
| config = db.getMongo().getDB("config"); | |
| config.shards.find().forEach( | |
| function (shard,_a,_i) { | |
| connections.push(new Mongo(shard["host"])); | |
| } | |
| ); | |
| db.getMongo().getDB(database).getCollection(collection).find().forEach( | |
| function (doc, _a, _i) { | |
| count = 0; | |
| onshards = []; | |
| connections.forEach( | |
| function(con, _a, _i) { | |
| if (con.getDB(database).getCollection(collection).find(doc).count()==1) { | |
| count += 1; | |
| onshards.push(con); | |
| } | |
| } | |
| ); | |
| if (count > 1) { | |
| print("duplicate doc: "+doc['_id']+" found on: "); | |
| onshards.forEach(function(shard,_a,_i) {print(shard.toString())}); | |
| } | |
| } | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment