Skip to content

Instantly share code, notes, and snippets.

@normgraham
Created January 30, 2014 19:33
Show Gist options
  • Select an option

  • Save normgraham/8716967 to your computer and use it in GitHub Desktop.

Select an option

Save normgraham/8716967 to your computer and use it in GitHub Desktop.
Prints a list of duplicate keys (here as the Value field in the StringLookup collection of all dbs) that are strings shorter than the maximum key length of 1024 taking overhead into account, which makes the maximum string key length to be 1011.
var printDupValidKeys = function() {
db.adminCommand({"listDatabases":1}).databases.forEach(
function(sdb) {
print(sdb.name);
try {
dupes=db.getSiblingDB(sdb.name).StringLookup.aggregate(
[ { $group: {
"_id" : "$Value",
"c": { "$sum" : 1 } } },
{ $match : {
"c" : {"$gt" : 1 } } } ] );
dupes.result.forEach(
function(dupe) {
if (dupe._id.length < 1012)
printjson(dupe);
});
}
catch (e) { }
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment