Upload file (size <16MB) to MongoDB

928 views
Skip to first unread message

Ashok Padmaraju

unread,
Oct 13, 2016, 6:52:20 AM10/13/16
to mongodb-user

I have a requirement to upload file to MongoDB. Currently I am saving files in a folder in current filesystem using Flask. Is there a way I can upload file to MongoDB without using GridFS? I believe I did something like this long before but I cannot recollect since its been longtime since I last used MongoDB.


Any file I select to upload is no more than 16MB in size.


Thank you.

Lukas Lehner

unread,
Oct 15, 2016, 3:13:05 PM10/15/16
to mongod...@googlegroups.com
you can store binData in BSON documents


or do I understand wrong and you search for a GUI tool to upload binData to server?

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/a438cc78-343e-4490-acb0-5b82332f5ddd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ashok Padmaraju

unread,
Oct 15, 2016, 9:01:17 PM10/15/16
to mongodb-user

Hello Lukas, Thank you for your reply. I tried the below code using binData (honestly I'm not sure if the syntax is correct. I'm new to both python and mongodb) but it throws error 'global name binData is not defined'. I have imported bson but still this error is showing up. Not sure if I'm using it properly. Basically I want to store image (less than <16mb) in mongoDB.



Thank you,



On Saturday, October 15, 2016 at 12:13:05 PM UTC-7, Lukas Lehner wrote:
you can store binData in BSON documents


or do I understand wrong and you search for a GUI tool to upload binData to server?
On Thu, Oct 13, 2016 at 9:52 AM, Ashok Padmaraju <raju....@gmail.com> wrote:

I have a requirement to upload file to MongoDB. Currently I am saving files in a folder in current filesystem using Flask. Is there a way I can upload file to MongoDB without using GridFS? I believe I did something like this long before but I cannot recollect since its been longtime since I last used MongoDB.


Any file I select to upload is no more than 16MB in size.


Thank you.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.

Wan Bachtiar

unread,
Oct 18, 2016, 8:30:38 PM10/18/16
to mongodb-user

. Is there a way I can upload file to MongoDB without using GridFS?

Hi Ashok,

Could you share the reason why you prefer not to use MongoDB GridFS in this case ?

GridFS handles splitting binary data into 255kB (default) chunk size, while at the same time allowing you to specify custom metadata.

Based on your example, you could use GridFS as below:

import pymongo
import gridfs
to_upload = "Headshot.jpg"
client = pymongo.MongoClient()
fs = gridfs.GridFS(client.dbName)
# Store in dbName.fs
fs.put(open(to_upload, 'r'), content_type='image/jpeg', filename=to_upload)

You can then check out the metadata document in (the chunks would reside in db.fs.chunks ): 

> use dbName;
> db.fs.files.find();
{
  "_id": ObjectId("5806b9dd82616977302bff19"),
  "contentType": "image/jpeg",
  "chunkSize": 261120,
  "filename": "Headshot.JPG",
  "length": 649392,
  "uploadDate": ISODate("2016-10-19T00:10:05.674Z"),
  "md5": "50074451f3fd75267bdf4434e504e7e0"
}

See also:

If you still would like to store the binary data, you could use the binary bson type as suggested by Lukas.

Based on your example :

import pymongo
import bson
to_upload = "Headshot.JPG"
client = pymongo.MongoClient()
coll = client.dbName.collectionName
with open(to_upload, "r") as fh: 
    bindata = bson.binary.Binary(fh.read())
coll.insert({'filename': to_upload, 'file': bindata})

The snippet above is based on PyMongo v3.3.0 and MongoDB v3.2.

Regards,

Wan.

Reply all
Reply to author
Forward
0 new messages