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)
.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With