if we want to delete gridfs file from mongo db with chunks and file then use below script:
mydoc: is my collection that contain the reference of gridFsFileId for each document
as i want to delete all document below or equal from a particular date:
db.mydoc.find({ "creationTime": { $lte: NumberLong(1485172670719)}}).forEach(
function(mydoc){
if(mydoc!==null){
//remove chunk in loop
db.fs.chunks.remove({
"files_id": ObjectId(mydoc.gridFsFileId)
});
//remove file
db.fs.files.remove({
"_id": ObjectId(mydoc.gridFsFileId)
});
//remove from mydoc
db.mydoc.remove({
"gridFsFileId": mydoc.gridFsFileId
});
}
}
);
mydoc: is my collection that contain the reference of gridFsFileId for each document
as i want to delete all document below or equal from a particular date:
db.mydoc.find({ "creationTime": { $lte: NumberLong(1485172670719)}}).forEach(
function(mydoc){
if(mydoc!==null){
//remove chunk in loop
db.fs.chunks.remove({
"files_id": ObjectId(mydoc.gridFsFileId)
});
//remove file
db.fs.files.remove({
"_id": ObjectId(mydoc.gridFsFileId)
});
//remove from mydoc
db.mydoc.remove({
"gridFsFileId": mydoc.gridFsFileId
});
}
}
);
No comments:
Post a Comment