我应该使用 GridFS 还是二进制数据来存储和存储数据?从 MongoDB 检索图像?

发布于 2024-12-10 16:16:10 字数 107 浏览 2 评论 0原文

我想知道哪个更好/更快:

  1. 拥有一个单独的文档集合,其中仅包含保存为二进制数据的图像,可能还包含一些元数据。
  2. 或者使用GridFS来存储图像。

I was wondering which is better/faster:

  1. Having a separate collection of documents that just contain the image saved as binary data, and possibly some metadata.
  2. Or using GridFS to store the images.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

眸中客 2024-12-17 16:16:10

如果您的图像很小,您可以将它们作为二进制数据存储在集合中的文档中。只需考虑一下,每次查询文档时都会检索它们(除非您从查询中排除“图像”字段)。

但是,如果您的图像较大,我会使用 GridFS。 GridFS 具有一些使其非常擅长处理图像的功能,您应该考虑这些功能:

  • 对于较大的图像,当它们存储在 GridF 中时,它们将被分割成块,您可以存储非常大的文件。如果您尝试在文档中存储图像,则会受到文档最大大小 16Mb 的限制,并且您正在消耗实际文档需要使用的空间。
  • 您可以将元数据添加到图像本身,并对这些属性运行查询,就像从集合中的常规文档执行此操作一样。因此 GridFS 与图像元数据文档一样好。
  • 我真的很喜欢在图像上计算 MD5 哈希值。 (这对于我的一些案例非常有用)。
  • 通过将图像存储在 GridFS 中,您可以将图像预处理为二进制格式(这没什么大不了的,但 GridFS 很方便)。

在性能方面,针对常规文档的读/写应该与针对 GridFS 的读/写没有什么不同。我不认为性能是选择任何一个的区别因素。

我个人的建议是使用 GridFS,但您需要针对您的特定用例进行分析。

希望这有帮助。

If your images are small you can store them as binary data in the documents in your collection. Just consider that you will be retrieving them every time you query your document (unless you exclude the 'image' field from your queries).

However, if your images are larger I would use GridFS. GridFS has some features that make it very good at handling images that you should consider:

  • For larger images, when they are stored in GridFs they will be split in chunks and you can store very large files. If you try to store images in your document, you are constrained by the 16Mb max size of a document, and you are consuming space that needs to be used for your actual document.
  • You can add metadata to the image itself and run queries against these attributes, as if you were doing it from a regular document in a collection. So GridFS is as good as a document for metadata about the image.
  • I really like that I get MD5 hash calculated on the images. (It is very useful for some of my cases).
  • By storing images in GridFS you save yourself the preprocessing of the image into binary format (not a big deal, but a convenience of GridFS)

In terms of performance, reading/writing against a regular document should be no different than doing it against GridFS. I would not consider performance to be a differentiator in choosing either one.

My personal recommendation is to go with GridFS, but you need to analyze for your particular use case.

Hope this helps.

瞳孔里扚悲伤 2024-12-17 16:16:10

我使用 GridFS 来存储照片和文档。它非常简单,从集合中检索它以在本地显示或保存也很容易。您可以将元数据与二进制数据一起存储在同一集合中。这样您就不需要创建额外的集合来存储它们。

例如,在我的一个项目中,我存储用户个人资料照片以及用户名、文件类型和上传日期。

I use GridFS to store photos and documents. It's so easy and retrieving it from the collection to display or save locally is easy. You can store metadata along w/ the binary data inside the same collection. This way you don't need to create an additional collection to store them.

For example, in one of my project I store user profile photos along with usernames, file type, and date of upload.

星星的軌跡 2024-12-17 16:16:10

GridFS 的开发是为了以有效的方式处理文件。
http://www.mongodb.org/display/DOCS/When+to +use+GridFS

不要忘记您可能需要翻译数据
到一个文件并返回。

但可以肯定的是,请根据您的使用模式进行性能测试。

GridFS is developed to handle Files in an efficient way.
http://www.mongodb.org/display/DOCS/When+to+use+GridFS

Do not forget that you maybe will have to translate the data
to a file and back.

But to be sure, do a performance test that takes account of your usage pattern.

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