dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/mongodb/#86
Script #86
mongodb
Data Qualityread-onlyMin Version: 3.6

Find Duplicate Documents in MongoDB

Lists repeated values of a field with the related _id values. Required data cleanup before creating a unique index.

#duplicates#unique-index#aggregation
mongodb · javascript
// "users" koleksiyonunda email alanı tekrar edenleri bul
db.users.aggregate([
  { $group: { _id: "$email", count: { $sum: 1 }, ids: { $push: "$_id" } } },
  { $match: { _id: { $ne: null }, count: { $gt: 1 } } },
  { $sort: { count: -1 } },
  { $limit: 50 }
], { allowDiskUse: true });
Operational Notes & Remediation Guidance

Duplicate documents can be cleaned up or prevented by creating a unique index on key fields.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 3.6).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.