Store files on disk or MongoDB

2020-02-28 05:53发布

I am creating a mongodb/nodejs blogging system (similar to wordpress).

I currently have the images being saved on the disk and a pointer being placed in mongo. I was wondering since I have all sessions being stored in MongoDB to enable easy load balancing across servers, would storing the actual files in Mongo also be a smart idea for easy multiserver setups and/or performance gains.

If everything is stored in a DB, you can simply spawn more web servers and/or mongo replications to scale horizontally

Opinions?

3条回答
啃猪蹄的小仙女
2楼-- · 2020-02-28 05:56

MongoDB is a good option to store your files (I'm talking about GridFS), specially for the use case you described above. When you store files into MongoDB (GridFS, not documents), you get all the replication and sharding capability for free, which is awesome.

If you have to spawn a new server and you have the files already into MongoDB, all you have to do is to enable replication (thus scale horizontally). I'm sure this can save you a lot of headaches.

Resources:

Is GridFS fast and reliable enough for production?
http://www.mongodb.org/display/DOCS/GridFS
http://www.coffeepowered.net/2010/02/17/serving-files-out-of-gridfs/

查看更多
Rolldiameter
3楼-- · 2020-02-28 06:07

Aside from GridFS, you might be considering a cloud-based deployment. In that case, you might consider storing files in cloud-specific storage (Windows Azure has Blob Storage, for example). Sticking with Windows Azure for this example (since that's what I work with), you'd reference a file by its storage account URI. For example:

https://mystorageacct.blob.core.windows.net/mycontainer/myvideo.wmv

Since you'd be storing the MongoDB database itself in its own blob (and mounted as disk volume on your Linux or Windows VM), you could then choose to store your files in either the same storage account or a completely different storage account (with each storage account providing 100TB 200TB of storage).

查看更多
Melony?
4楼-- · 2020-02-28 06:13

Storing the image as document in mongodb would be a bad idea, as the resources which could have been used to send a large amount of informational data would be used for sending files.

Have a look at mongoDb file storage GridFS , that might solve your problem of storing images, and providing horizontal scalability as well.

http://www.mongodb.org/display/DOCS/GridFS http://www.mongodb.org/display/DOCS/GridFS+Specification

查看更多
登录 后发表回答