Store C structure into MongoDB

214 views
Skip to first unread message

Dayakar Reddy

unread,
Sep 5, 2015, 7:58:23 PM9/5/15
to mongodb-user
Hi,

My requirement is to store C struct as a part of document into MongoDB database.

For example:

 struct looks like:
     typedef struct student
{
int employee;
char name[50];
unsigned char status;
int totmarks;
}
 
 I would like to add two fields in the JSON document. One field would be unique ID and second field would be above filled structure(student data). 
 
 Can you please guide me in storing the above JSON document into MongoDB database?


Regards,
Dayakar

Wan.Bachtiar

unread,
Sep 7, 2015, 4:06:15 AM9/7/15
to mongodb-user
Hi Dayakar, 

You could store the JSON document in a structure like below:

{
 students
: {
 
"employee": 99,
 
"name": "myname",
 
"status": "mystatus",
 
"totmarks": 99
 
}
}


In regards to the unique ID, one way is to use the default MongoDB document id. All MongoDB drivers will add the _id field, and assign a unique id by default. 

For example:

{
 _id
: ObjectID("55ed34ef4a2e2b4416656fa1"),
 students
: {
 
"employee": 99,
 
"name": "myname",
 
"status": "mystatus",
 
"totmarks": 99
 
}
}



Assuming you are using the latest MongoDB C driver in your application, there are two ways of constructing the BSON document. The first way is to use BSON directly, but there is also an alternative, BCON. BCON is meant to provide convenient syntactic sugar to allow inline construction of complex BSON objects in a declarative manner. 


BSON example :

#include <mongoc.h>
#include <bson.h>
   
bson_t
*doc;  
doc
= bson_new ();


bson_t subdoc
;
BSON_APPEND_DOCUMENT_BEGIN
(doc, "students", &subdoc);
BSON_APPEND_INT32
(&subdoc, "employee", 99);
BSON_APPEND_UTF8
(&subdoc, "name", "myname");
BSON_APPEND_UTF8
(&subdoc, "status", "mystatus");
bson_append_document_end
(doc, &subdoc);



BCON example :

#include <mongoc.h>
#include <bson.h>
   
bson_t
*doc;


doc
= BCON_NEW ("students", "{",
 
"employee", BCON_INT32(99),
               
"name", BCON_UTF8("myname"),
               
"status", BCON_UTF8("mystatus"),
                 
"}");



You can find more C examples on this page.

Regards,

Wan.Bachtiar

unread,
Sep 13, 2015, 8:52:44 PM9/13/15
to mongodb-user
> Hi Wan, Thanks for your response.
> I have tried the first approach that you have suggested i.e to use BSON_APPEND_* methods to store the members of C sturcture.
> This method id ok if structure contains less number of items in C structure. In my case, the structure would contain around 150 members. Therfore it is tedious
> to use BSON_APPEND_* methods for all members of structure.
> Is there any BSON_APPEND_* API which can take object/structure as parameter to store into MongoDB?
> Can you please guide me?


Hi Dayakar,

There is no existing method that can take an arbitrary C struct object, iterate through its members, and automatically convert them to a JSON/BSON object. 
You have to write a serialisation method of your C struct object to JSON/BSON.

To help dig more into this, would you be able to provide more information please ?

1. Where is the data coming from ? i.e. files or database.
2. What format the data is in ? i.e. JSON, CSV, binary ?


Regards,

Wan

Dayakar Reddy

unread,
Sep 14, 2015, 12:57:36 AM9/14/15
to mongodb-user
Hi Wan

Thanks for your response!!!

Currently, the C structure that I was referring to store my application state. To save the state of application We used to serilize this Structure into file system regularly during my application processing.

Now, Instead of storing the structure into file system I intend to store in MongoDB(BSON format) to achieve high availability.

Please see my inline comments.

1. Where is the data coming from ? i.e. files or database.
[Dayakar]: Data is coming from my application.


2. What format the data is in ? i.e. JSON, CSV, binary ?
[Dayakar]: Format of data is C structure.

What do you think our approach should be in this case?

Regards,
Dayakar

Wan Bachtiar

unread,
Sep 14, 2015, 9:33:23 PM9/14/15
to mongodb-user
Hi Dayakar, 

Thanks for the information. 

Since there is no [type introspection](https://en.wikipedia.org/wiki/Type_introspection) in C, 
you would have to write your own method to serialize your C struct into BSON. 


Regards, 

Wan 
Reply all
Reply to author
Forward
0 new messages