Reputation: 47776
I have files stored in a MongoDB using GridFS. I need to remove some of those files by ID, from the JavaScript shell. I need to remove a single file using it's ID. I figured I could just do this:
db.fs.files.remove({_id: my_id});
This works to some extent; it removes the file from the fs.files
collection but does not remove the chunks itself from the fs.chunks
collection. The reason I know that is because I check the length of both collections before and after in RockMongo.
I could go through the chunks and remove those that are referring to that file, but is there a better, built-in way of doing that?
Upvotes: 15
Views: 19839
Reputation: 31
mongofiles --host localhost:30000 --db logo delete logo_susan1225.png
refer to this page: http://docs.mongodb.org/manual/reference/program/mongofiles/#bin.mongofiles
Upvotes: 3
Reputation: 995
You can delete gridFS file by deleting both chunks and files from shell. for example
db['fs.chunks'].remove({files_id:my_id});
db['fs.files'].remove({_id:my_id});
Those commands will do such trick.
Upvotes: 26
Reputation: 33175
You want to use db.fs.delete(_id);
instead.
Update Sorry, that apparently doesn't work from the shell, only through the driver. GridFS is a specification for storage implemented by the drivers. Looks like it doesn't have much built-in functionality from the shell, as such.
Update 2 There is also a command line tool, mongofiles (http://www.mongodb.org/display/DOCS/GridFS+Tools), which allows you to delete files by name. mongofiles delete <filename>
. It comes with a warning that it will delete all of the files by that name, so it's not as granular as by id.
Upvotes: 15