MongoDB implementation file deletion
Do Itoo in the process, in the implementation of the MongoDB file deletion when encountering point problems, Internet search, many online resources, want to find out what they want is not easy, so can only try, good mentality, pondering a few days to achieve the deletion of files according to the ID, but today looked at, delete has a place to optimize, is the multi-select Delete, many times prompt deletion success, so the bite-yigujin to solve it, this really want to thank Jiege, helped me a lot of busy AH
This is the core code that the foreground removes:
Deletes the selected file $ ("#btndelete") according to the file ID. Click (function () {///To determine if the file to be deleted is selected var rows = $ ('
#fileList '). DataGrid (' getselected '); if (!rows | | rows = = NULL) {$.messager.alert ("Prompt", "Select the file you want to delete.
");
Return
} var files = $ (' #fileList '). DataGrid (' Getselections ');
var obj = eval (files);
var fileid = ';
Intercept Fileid for (var i = 0; i < files.length; i++) {obj[i]._id + = ' | ';
Fileid + = obj[i]._id;//Get ID} console.log (Fileid); Transfer data, execute the controller's Delete method $.ajax ({type: ' POST ', url: '/mongodbhe Lp/processrequest ', data: {action: ' DELETE ', Value:fileid}, Success:function (DA TA) {
GetFiles ();
Alert ("Delete file succeeded");
}, Error:function (err) {alert (ERR);
}
}); })
Background code:
<summary>///delete files according to Objectid///</summary>//<returns></re turns> public void Delete () {//Gets the array of the foreground string filesid = Reques
t["value"]; Intercept array element string[] File = Filesid.
Split (' | ');
Remove the last set of ' | ' list<string> fileList = file.
ToList ();
Filelist.remove (Filelist[filelist.count-1]);
Chaku mongogridfssettings fssetting = new Mongogridfssettings () {Root = fileTable};
Mongogridfs fs = new Mongogridfs (DB, fssetting); Mongocollection collection = DB.
GetCollection (fileTable + ". Files"); Traverse Delete foreach (string item in fileList) {var result = collection.
Remove (Query.eq ("_id", New ObjectId (item));
}}
Summarize:
Try more, you need more guts.