3

Mongoose

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

Here I created a collection("dbmanager") for saving files. When I try to remove file from dbmanager collection, the document will remove from "dbmanager.files". But documents still showing in "dbmanager.chunks". I need to remove that documents from "dbmanager.files" and "dbmanager.chunks".

Naveen
  • 757
  • 3
  • 17
  • 41

1 Answers1

3

I just did this recently using gridfs-stream

Then the gfs driver does it for you:

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

or

gfs.remove({ _id: fileId });
dyouberg
  • 2,206
  • 1
  • 11
  • 21
  • It's in their github code here: https://github.com/aheckmann/gridfs-stream/blob/c394e050d066a5c81c6dcdddad186b08a93d5f1f/lib/index.js#L87 – dyouberg Nov 04 '19 at 14:58
  • it no longer works, I get DeprecationWarning: collection.remove is deprecated. Use deleteOne, deleteMany, or bulkWrite instead. – Curious Flower Aug 13 '20 at 09:15