7

I want to save uploaded files such as a user's avatar image to user collection in MongoDB, I guess the steps are:

  1. user upload a image;
  2. use a FileHandler (IHttpHandler, Asp.net) to accept the file stream in web server;
  3. then I get that file binary and set it to a User class object
  4. finally save the object, so the user collection will get the file binary data.

Am I right about this? Could you give me any clue?

lotus_misser
  • 135
  • 1
  • 1
  • 10
  • What client language are you using (I guess C# or something, but I'm not sure)? – cutsoy Sep 24 '11 at 11:17
  • yeah, I used C#, I know GridFs maybe one solution, but it just for big file store and convenient to use shard or replica something, I search google and can't find similar situation I face. – lotus_misser Sep 24 '11 at 12:03
  • I got a good reference for converting binary data to img in browser: http://forums.asp.net/p/1505985/3574567.aspx – lotus_misser Sep 24 '11 at 13:11
  • `GridFS` isn't only for storing big files. One thing to bear in mind if you store files directly in your user document is that you'll have to load and transmit that data every time you query for the user, rather than only as needed. Conversely, you have to load the rest of the user document when all you need is the file. – dcrosta Sep 24 '11 at 14:01
  • yeah, I think you are right. what I thought is retrive user data once for whole including avator binary, then the View page get the Model and combine the data for img tag---which I think now is wrong, you can't transmit binary data over html protocol, to handler this extra work should be done, convert binary to text which may be store in hidden input, and next step also is a tough one, turn there text to a img object, this feasibility reminds vague. and another way out I think is to use extra collection for storing images for the reason @dcrosta said, btw. thanx – lotus_misser Sep 24 '11 at 16:07
  • about GridFS: it requires two queries: one to fetch a file’s metadata and one to fetch its contents, it raises db server's pressure, when application become vary large, i think it's a good substitution of file system for the loading efficience and the data backups. – lotus_misser Sep 24 '11 at 16:07
  • As a sidenote, here's what MongoDB advise as to [when to use GridFS](http://www.mongodb.org/display/DOCS/When+to+use+GridFS). – ZogStriP Dec 20 '11 at 16:19

2 Answers2

6

You should just use GridFS to do it - there is no reason why you should worry about files being too large or too small for it to make sense. GridFS was designed to store files so you should use it.

You could always store a reference to the GridFS file in a normal MongoDB document if you need to, and then load it out of GridFS when you need to use it.

See this thread: MongoDB GridFs with C#, how to store files such as images?

Community
  • 1
  • 1
Bryan Migliorisi
  • 8,982
  • 4
  • 34
  • 47
4

According to Mongo grid fs documentation. If your files are all smaller than the 16 MB BSON Document Size limit, consider storing each file in a single document instead of using GridFS. You may use the BinData data type to store the binary data. See your drivers documentation for details on using BinData.