Clean Duplicate Metadata Values in MongoDB
Clean Duplicate Metadata Values in MongoDB
Original Question or Issue:
Environment:
- Product - FileCloud Server,
- Version -
- Platform - Windows, Linux
Steps to Reproduce:
Error or Log Message:
Defect or Enhancement Number:
Cause:
Resolution or Workaround:
Connect to MongoDB using mongosh
Switch to the FileCloud database that contains the metadata_values collection
Find duplicate values by using this:
db.metadata_values.aggregate([ { $group: { _id: "$fullpath", count: { $sum: 1 }, duplicates: { $addToSet: "$_id" } } }, { $match: { count: { $gt: 1 } } }, { $project: { _id: 0, path: "$_id", duplicates: 1 } }]);
Clean the duplicated values:
db.metadata_values.aggregate([{ $group: { _id: "$fullpath", ids: { $push: "$_id" }, count: { $sum: 1 } } },{ $match: { count: { $gt: 1 } } }, { $project: { ids: { $slice: ["$ids", 1, { $size: "$ids" }] } } }]).toArray().forEach(doc => db.metadata_values.deleteMany({ _id: { $in: doc.ids } }));
Notes: