我可以通过Node JS中的发布请求将大于16MB的文本文件上传到MongoDB
我正在建立一个网站,用户将他们的详细信息和工作上传到数据库,但他们的工作是文本、视频和图像的组合。我不知道如何使用 multer 将此类文件立即上传到 mongodb。应该有人帮忙。
我尝试在不使用 multer 的情况下上传它,但是当文档超过 16mb 时,它会显示错误。
I am building a website where users upload their details and work to the database but their work is a combination of text, videos and images. And I don't know how to upload such file to the mongodb at once with multer. Someone should please help.
I tried uploading it without multer but when a document exceed 16mb it displays an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
单个mongoDB文档的数量不超过16MB。 Multer不是问题所在。
幸运的是,有一个解决方案可以在mongodb中存储大文件: gridfs 。
该文件分解成小于16MB的块,然后存储在单独的集合中。而不是这样:
您要保存这样的东西:
中work.files
。您无需自己创建这些集合,也无需自己构成部分零件,因为有库可以帮助您解决此问题,,例如mongoose-gridfs 。大多数库都提供了将两个传输文件传输到GRIDFS集合中的方法,因此它也可以非常具有性能。
对于非常小的文件(例如头像),将它们嵌入在文档中可能仍然有利。
附带说明:关于Gridfs的“特殊”并不是很多“特殊” - 您不是需要安装的扩展名,而是许多人遵循的互操作性。这些文档存储在标准的普通MongoDB集合中。 As such you can store any metadata/ custom fields (example在
(whyt)中,从前面提到的库中
).files
(如果需要)。取决于要存储的文件的大小。如果要流式传输它们(例如,视频),则可能需要调整块尺寸(默认255kb) - 每文件。A single MongoDB document can be no more than 16MB. Multer is not the problem here.
Luckily there is a solution to storing large files in MongoDB: GridFS.
The file is broken up into chunks that are smaller than 16MB and then stored in a separate collection. Rather than this:
You'd save something like this:
work.files
.You don't need to create these collections by yourself and there's no need to chunk the files up in parts yourself, as there are libraries to help you with this, such as Mongoose-GridFS. Most libraries provide methods to both stream files into and out of a GridFS collection, so it can be very performant, as well.
For very small files (such as an avatar) it might still be advantageous to store them embedded in your document.
A side note: There's not a lot "special" about GridFS—it's not an extension you need to install but more of a blessed standard that many people follow for interoperability. The documents are stored in standard, normal MongoDB collections. As such you can store any metadata/ custom fields (example from the previously mentioned library) in
(whatever).files
if you want. Depending on how large the files you're going to store are & if you're going to stream them (eg. video) you may want to tune chunk size (default 255kb)—even per file.