Skip to content

Instantly share code, notes, and snippets.

@fipar
Created June 10, 2015 16:19
Show Gist options
  • Select an option

  • Save fipar/4e196e24450b4f5afd92 to your computer and use it in GitHub Desktop.

Select an option

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).
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