2016-11-29 44 views
1

貓鼬如何從GridFS中的塊和文件中刪除文件?

var Grid = require('gridfs-stream'); 
var mongoose = require('mongoose'); 
var GridFS = Grid(mongoose.connection.db, mongoose.mongo); 
GridFS.collection('dbmanager').remove({_id: mongoose.Types.ObjectId(req.file_id)}, function (err) { 
    console.log("deleted"); 
}); 

在這裏,我創建了一個集( 「dbmanager」)保存文件。當我嘗試從dbmanager集合中刪除文件時,文檔將從「dbmanager.files」中刪除。但文檔仍然顯示在「dbmanager.chunks」中。我需要從「dbmanager.files」和「dbmanager.chunks」中刪除這些文檔。

回答

1

我只是做這個最近使用gridfs-stream

然後GFS驅動程序會爲你:

gfs.remove(options, function (err) { 
    if (err) return handleError(err); 
    console.log('success'); 
}); 

gfs.remove({ _id: fileId });