在没有GridFS的NodeJS中使用MongoDB存储一些小的文件(小于1MB) [英] Storing some small (under 1MB) files with MongoDB in NodeJS WITHOUT GridFS

查看:190
本文介绍了在没有GridFS的NodeJS中使用MongoDB存储一些小的文件(小于1MB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经营一个在nodeJS + mongoDB后端运行的网站.现在,我正在实现一个系统来存储一些需要存储在数据库中的图标(小的图像文件).

I run a website that runs on a backend of nodeJS + mongoDB. Right now, I'm implementing a system to store some icons (small image files) that will need to be in the database.

根据我的理解,不使用GridFS更有意义,因为它似乎是为大型文件或大量文件量身定制的.由于我需要保存的每个文件都将在BSON最大文件大小以下,因此我应该能够将它们直接保存到常规文档中.

From my understanding, it makes more sense NOT to use GridFS, since that seems to be tailored for either large files or large numbers of files. Since every file that I need to save will be well under the BSON maximum file size, I should be able to save them directly into a regular document.

我有2个问题:

1)我的推理正确吗?与GridFS相比,可以将图像文件保存在常规mongo集合中吗?我在这里没有考虑应该做的事情吗?

1) Is my reasoning correct? Is it ok to save image files within a regular mongo collection, as opposed to with GridFS? Is there anything I'm not considering here that I should be?

2)如果我的思考过程是正确的,我该如何去做?我可以做以下事情吗?

2) If my thought process is sound, how do I go about doing this? Could I do something like the following:

//assume 'things' is a mongoDB collection created properly using node-mongodb-driver

fs.readFile(pathToIconImage, function(err,image){
  things.insert({'image':image}, function(err,doc){
    if(err) console.log('you have an error! ' + err);
  });
});

我猜想可能有更好的方法,因为mongoDB使用BSON,在这里我试图在将文件发送到数据库之前将其保存为JSON.我也不知道这段代码是否可以工作(还没有尝试过).

I'm guessing that there's probably a better way to do this, since mongoDB uses BSON and here I'm trying to save a file in JSON before I send it off to the database. I also don't know if this code will work (haven't tried it).

更新-新问题

如果我在一个收藏夹中保存了三个信息的文档:1)名称,2)日期和3)图像文件(上面的图标),并且我想将此文档发送到客户端以显示所有三个,这可能吗?如果没有,我想我需要使用GridFS并保存fileID来代替图像本身.有想法/建议吗?

If I have a document within a collection that has three pieces of information saved: 1) a name, 2) a date, and 3) an image file (the above icon), and I want to send this document to a client in order to display all three, would this be possible? If not, I guess I'd need to use GridFS and save the fileID in place of the image itself. Thoughts/suggestions?

最好,谢谢您的答复,
萨米(sami)

Best, and thanks for any responses,
Sami

推荐答案

如果您的图像确实足够小,不会对文档大小造成影响,并且您不介意进行少量额外处理,那么可以只需将其直接存储在您的收藏夹中即可.为此,您需要对图像进行base64编码,然后使用mongo的BinData类型存储它.据我了解,它将被保存为BSON位数组,而不是实际存储base64字符串,因此大小不会增长到大于原始二进制映像.

If your images truly are small enough to not be a problem with document size and you don't mind a little amount of extra processing, then it's probably fine to just store it directly in your collection. To do that you'll want to base64 encode the image, then store it using mongo's BinData type. As I understand it, that will then save it as a BSON bit array, not actually store the base64 string, so the size won't grow larger than your original binary image.

它将在json查询中显示为base64字符串,您可以使用该字符串将二进制图像取回.

It will display in json queries as a base64 string, which you can use to get the binary image back.

这篇关于在没有GridFS的NodeJS中使用MongoDB存储一些小的文件(小于1MB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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