如何在MongoDB中存储文件? [英] How to store files in MongoDB?

查看:979
本文介绍了如何在MongoDB中存储文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人告诉我MongoDB使用GridFS文件系统来存储文件.但是,是否可以通过序列化或base64编码将文件本身存储在数据库BSON结构中?

I have been told MongoDB uses the GridFS filesystem for storing files. However, Is there any way to store the file themselves in the database BSON structure by serializing or base64 encoding ?

感谢并感谢您的帮助.

推荐答案

当然,您可以将文件序列化为字符串并将其存储在document字段中.但是这种方法有一个缺点-您的所有文件都应<然后是16 mb(一个文件的大小限制).

Sure, you can serialize your file as string and store it in document field. But in such approach there is one downside - all your files should < then 16 mb (size limit of one document).

Mongodb gridfs构建在mongodb集合之上,并且包含两个集合:

Mongodb gridfs build on top of mongodb collections, and contains two collections:

  1. 文件-文件的元数据(名称,大小,id,要保存的任何其他信息)
  2. Chunks-包含文件序列化主体的集合.如果文件大小超过16 MB,则mongodb会自动创建其他块.因此,任何文件都将作为一个文件文档和> = 1块文档保留在mongodb中.

您可以检查这里文件在mongodb中的样子.

You can check here how file looks like in mongodb.

因此,如果您决定将文件存储在mongodb中,则最好使用经过社区方法测试的通用方法-gridfs.

So, if you decided to store files in mongodb it is better to use common, tested by community approach - gridfs.

这篇关于如何在MongoDB中存储文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆