我可以通过Node JS中的发布请求将大于16MB的文本文件上传到MongoDB

发布于 2025-01-17 13:28:50 字数 151 浏览 1 评论 0原文

我正在建立一个网站,用户将他们的详细信息和工作上传到数据库,但他们的工作是文本、视频和图像的组合。我不知道如何使用 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 技术交流群。

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

发布评论

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

评论(1

笔落惊风雨 2025-01-24 13:28:51

单个mongoDB文档的数量不超过16MB。 Multer不是问题所在。

幸运的是,有一个解决方案可以在mongodb中存储大文件: gridfs

该文件分解成小于16MB的块,然后存储在单独的集合中。而不是这样:

{
  _id: ...,
  name: "John Smith",
  work: [{
    file: (a very large binary file),
    filename: "foo.mp4"
  }]
}

您要保存这样的东西:

  1. Gridfs将在名为的集合中创建一个文档,例如'work.files'
{
  _id: ...,
  filename: "foo.mp4",
  ...
}
  1. Gridfs将在一个名为fim的集合中创建多个文档,每个文档小于16mb,例如'work.chunks'
{
  _id: ...,
  files_id: (a reference to a work.files._id),
  n: 1,
  data: ...
}, {
  _id: ...,
  files_id: (the same reference to a work.files._id),
  n: 2,
  data: ...
}
  1. 而不是嵌入式文件,您将对条目的参考存储在中work.files
{
  _id: ...,
  name: "John Smith",
  work: [{
    _id: (a reference to a work.files._id)
  }]
}

您无需自己创建这些集合,也无需自己构成部分零件,因为有库可以帮助您解决此问题,,例如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:

{
  _id: ...,
  name: "John Smith",
  work: [{
    file: (a very large binary file),
    filename: "foo.mp4"
  }]
}

You'd save something like this:

  1. GridFS will create a document in a collection named, for example 'work.files'
{
  _id: ...,
  filename: "foo.mp4",
  ...
}
  1. GridFS will create multiple documents, each smaller than 16MB, in a collection named for example 'work.chunks'
{
  _id: ...,
  files_id: (a reference to a work.files._id),
  n: 1,
  data: ...
}, {
  _id: ...,
  files_id: (the same reference to a work.files._id),
  n: 2,
  data: ...
}
  1. Rather than the embedded file, you store a reference to the entry in work.files.
{
  _id: ...,
  name: "John Smith",
  work: [{
    _id: (a reference to a work.files._id)
  }]
}

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.

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