Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB deleting fs.chunks where not in fs.files

Tags:

mongodb

I have 10 GB of data in fs.chunks and I want to delete every document that is not on fs.files. I already removed every entry in fs.files I don't want so every id in fs.files is a file I want to keep.

So, I want something like db.fs.chunks.remove({"_id": {$nin: fs.files._id}}) or "delete every entry in fs.chunks that doesn't exist in fs.files".

Edit: I'm looking for the mongo equivalent of SQL delete from fs_chunks where id not in (select id from fs_files).

like image 322
islon Avatar asked Mar 16 '12 13:03

islon


1 Answers

I don't think there's an easy way to do this apart from performing a find and then iterating with forEach. So something like:

function removeChunkIfNoOwner(chunk){
  //Look for the parent file
  var parentCount = db.fs.files.find({'_id' : chunk.files_id}).count();

  if (parentCount === 0 ){
     db.fs.chunks.remove({'_id': chunk._id});
     print("Removing chunk " + chunk._id);
  }
}

db.fs.chunks.find().forEach(removeChunkIfNoOwner);

You can see this approach should work if you create a function like this:

function listParentFile(chunk){
   var parent = db.fs.files.findOne({'_id' : chunk.files_id});
   printjson(parent);
}
db.fs.chunks.find().forEach(listParentFile);
like image 92
Mick Sear Avatar answered Sep 30 '22 17:09

Mick Sear