Re: how to save images in mongodb using c++

539 views
Skip to first unread message

David Hows

unread,
Sep 3, 2012, 7:12:14 PM9/3/12
to mongod...@googlegroups.com
Hi Gaurhari,

To save large files such as images in mongodb you need to use GridFS.
Information on GridFS is available here http://www.mongodb.org/display/DOCS/GridFS

You can see the full API implementation for GridFS in the C++ driver here

Cheers,

David

David Hows

unread,
Sep 5, 2012, 7:45:23 PM9/5/12
to mongod...@googlegroups.com
Hi Gaurhari,

GridFS does indeed store the image broken up over a number of documents in mongodb.

When you wish to store an object as a GridFS document in Mongo you would call the gridFS storeFile function which has two modes:
First is to load and store the file from an existing file in the OS 
Second is to load and store the file from a given array of data and the length of this data 

This second method should be what you are looking for. 

Please note that the API doc's aren't clear, you need to provide a fileName when using the second method so that gridFS has a file name reference for each image.


Cheers,

David

On Wednesday, September 5, 2012 9:56:20 PM UTC+10, gaur hari wrote:
Hi David,

Is there any way to convert images into binary form and store that in mongodb in c++ because in gridfs i need to define location of image but in case I am providing database as  sevice to deive and I want to upload image from client and save into database.how will I achieve that.??

gaur hari

unread,
Sep 9, 2012, 10:40:54 AM9/9/12
to mongod...@googlegroups.com
Thanks a lot David.

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

gaur hari

unread,
Sep 12, 2012, 2:42:56 PM9/12/12
to mongod...@googlegroups.com
Hi David,

Can you guide me how to create logs in c++ for mongodb query like in case of mongodb shell explain function is there to check the status of time,number of objects scanned,time taken to execute query same way how to implement in c++ ??.

Thanks & Regards
Gaurhari
Thanks a lot David.

David Hows

unread,
Sep 12, 2012, 9:48:29 PM9/12/12
to mongod...@googlegroups.com
Hi Gaurhari,

The C++ API contains an explain method which can be used to provide the same output as the shell explain method. 

Please have a look at the API documentation here:

Cheers,

David

gaur hari

unread,
Sep 13, 2012, 12:24:18 AM9/13/12
to mongod...@googlegroups.com
thanks David ,

one more question actually yesterday suddenly my mongodb server was corrupted due to unclean shutdown and when I tried to repair using repairdbpath I was getting message no such commad found after that i also gave argument as db/data0 then I was getting message this directory doesn't exist,then  i tried to remove lock file to repair mongod then I was getting message no lock file exist in that directory.

then I had to keep server on explicitly,only then it worked.

Thanks 
Gaurhari 


Cheers,

David

--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to

David Hows

unread,
Sep 13, 2012, 12:43:12 AM9/13/12
to mongod...@googlegroups.com
Hi Gaurhari,

Sounds like you missed the leading slash on the directory "/db/data0".

You can also run the repair command from within the database itself - without the need to restart.

Try executing "db.repairDatabase()" from within the shell.

Cheers,

David

gaur hari

unread,
Sep 13, 2012, 12:54:37 AM9/13/12
to mongod...@googlegroups.com
Hi David,

Thanks for immediate reply,just now I checked that  I had added /db/data0.

Anyway,Its working fine now if sometime happens next time I will use db.repairDatabase().

Thanks
Gaurhari


Cheers,

David

--

gaur hari

unread,
Sep 13, 2012, 10:35:33 AM9/13/12
to mongod...@googlegroups.com
Hi David,

I need your help.

I tried explain() fuction with QUERY but if I am right it only works with find query but I want to show logs like in case of inserting values for example i insert 5000 rows using c++ and I want to display how much time it took to complete that task. and how to show logs  using c++.

Thanks 
Gaurhari

David Hows

unread,
Sep 13, 2012, 7:10:08 PM9/13/12
to mongod...@googlegroups.com
Hi Gauhari,

None of the commands in mongodb return the amount of time taken to execute, with the obvious exception of explain on a query.

If you wish to capture the time taken to execute a command your best bet would be to capture a timestamp (epoch, microtime or nanotime) just prior to execution and then capture another as soon as you get a result. The difference of the two will be the time taken to execute.

Hope that helps,

David

gaur hari

unread,
Sep 13, 2012, 10:36:05 PM9/13/12
to mongod...@googlegroups.com
Hi David,

Thanks David,

yes I know about timestamps, ok I will try that but what about the logs if I want to know whether query was successful and want to display error in c++ program then how to access that error message of command and value of ok as false ?

Thanks
Gaurhari


David

--

David Hows

unread,
Sep 14, 2012, 12:51:36 AM9/14/12
to mongod...@googlegroups.com
Mongo does not offer any return value as to weather or not a command was successfully executed.

To actually confirm the status of the last update (or query) you will need to use the "getLastError" method.

The C++ driver provides two versions of this method
One will return the error as a string
The second will return the error as a bson object that includes the full error object.

API documentation on both is available:

Hope that helps,

David

gaur hari

unread,
Sep 14, 2012, 1:02:09 AM9/14/12
to mongod...@googlegroups.com
Hi David,

Thanks for the information,actually I know about getlasterror function but the problem is that like if I am executing multiple commands then I cant use getlasterror function every time due to performance issues,
that's why I was looking for some log kind of function,anyway thanks for the valuable suggestions.

Thanks
Gaurhari


Hope that helps,

David

--
Reply all
Reply to author
Forward
0 new messages