Skip to content

Instantly share code, notes, and snippets.

@xelaz
Created August 22, 2018 07:30
Show Gist options
  • Save xelaz/8aceeacc6306c5dd92f2c91699d89a27 to your computer and use it in GitHub Desktop.
Save xelaz/8aceeacc6306c5dd92f2c91699d89a27 to your computer and use it in GitHub Desktop.
Delete orphaned Chunks from Mongo GridFs
db.getCollection('fs.chunks').aggregate([
{ $lookup: { from: 'fs.files', localField: 'files_id', foreignField: '_id', as: 'file' } },
{ $unwind: { path:'$file', preserveNullAndEmptyArrays: true } },
{ $match: { file: { $exists: false } } },
{ $project: { _id: 1, files_id: 1 } },
]).forEach(function(chunk) {
printjson(chunk);
// db.getCollection('fs.chunks').remove({ _id: chunk._id });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment