Skip to content

Instantly share code, notes, and snippets.

@git-ff
Created January 22, 2018 18:22
Show Gist options
  • Save git-ff/3f9d634933d19e2f421cba5874ee51c0 to your computer and use it in GitHub Desktop.
Save git-ff/3f9d634933d19e2f421cba5874ee51c0 to your computer and use it in GitHub Desktop.
Delete duplicates mongo
// https://www.compose.com/articles/finding-duplicate-documents-in-mongodb/
db.hotels.aggregate([{ $group: { _id: { hid: "$hid" }, uniqueIds: { $addToSet: "$_id" }, count: {$sum: 1} } }, {$match: {count: {"$gt": 1}}}, {$sort: {count: -1}} ]).forEach(function(doc){ db.hotels.remove({ _id: doc.uniqueIds[1] }) });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment