MongoDB 从 shell 中删除 GridFS 对象

发布于 2024-12-26 05:04:28 字数 336 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巾帼英雄 2025-01-02 05:04:28

您可以通过从 shell 中删除块和文件来删除 gridFS 文件。例如

db['fs.chunks'].remove({files_id:my_id});
db['fs.files'].remove({_id:my_id});

那些命令就会做这样的事情。

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.

空城旧梦 2025-01-02 05:04:28

您想使用 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.

滥情哥ㄟ 2025-01-02 05:04:28
mongofiles --host localhost:30000 --db logo delete logo_susan1225.png

参考这个页面:
http://docs.mongodb.org/manual/reference/program /mongofiles/#bin.mongofiles

mongofiles --host localhost:30000 --db logo delete logo_susan1225.png

refer to this page:
http://docs.mongodb.org/manual/reference/program/mongofiles/#bin.mongofiles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文