MongoDB 从 shell 中删除 GridFS 对象
我使用 GridFS 将文件存储在 MongoDB 中。我需要从 JavaScript shell 中按 ID 删除其中一些文件。我需要使用它的 ID 删除单个文件。我想我可以这样做:
db.fs.files.remove({_id: my_id});
这在某种程度上有效;它从 fs.files
集合中删除文件,但不会从 fs.chunks
集合中删除块本身。我之所以知道这一点是因为我在 RockMongo 中检查了前后两个集合的长度。
我可以遍历这些块并删除那些引用该文件的块,但是有没有更好的内置方法来做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过从 shell 中删除块和文件来删除 gridFS 文件。例如
那些命令就会做这样的事情。
You can delete gridFS file by deleting both chunks and files from shell. for example
Those commands will do such trick.
您想使用 db.fs.delete(_id); 来代替。
更新
抱歉,这显然无法在 shell 中运行,只能通过驱动程序运行。 GridFS 是由驱动程序实现的存储规范。看起来它没有太多来自 shell 的内置功能。
更新2 还有一个命令行工具,mongofiles (http://www.mongodb .org/display/DOCS/GridFS+Tools),它允许您按名称删除文件。
mongofiles 删除<文件名>
。它附带一个警告,表示它将删除该名称的所有文件,因此它不像 id 那样精细。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.参考这个页面:
http://docs.mongodb.org/manual/reference/program /mongofiles/#bin.mongofiles
refer to this page:
http://docs.mongodb.org/manual/reference/program/mongofiles/#bin.mongofiles