I am trying to import a large json file into mongodb, one object at a time. a.json is a json file with a single record containing 15 key:value pairs. I am using the following code:
# include<stdio.h>
#include <bson.h>
#include <mongoc.h>
int main(void)
{
int i,j,f=0;
char c,s1[50],s2[50];
FILE *fp1,*fp2;
mongoc_client_t *client;
mongoc_collection_t *collection;
bson_error_t error;
bson_oid_t oid;
bson_t *doc=NULL;
bson_t *update = NULL;
bson_t *query = NULL;
mongoc_init ();
client = mongoc_client_new ("mongodb://localhost:27017/");
collection = mongoc_client_get_collection (client, "ClickAppJson", "ClickJson");
fp1=fopen("a.json","r");
while((c=getc(fp1))!=EOF)
{
if(c=='{')
{
do{
j=0;
c=getc(fp1);
do{
s1[j]= c;
j++;
c=getc(fp1);
}
while(c !=':');
s2[j]='\0';
c=getc(fp1);
j=0;
do{
s2[j]= c;
j++;
c=getc(fp1);
}
while(c !=','||c!='}');
s2[j]='\0';
if(f==0){
bson_oid_init (&oid, NULL);
doc = BCON_NEW ("_id", BCON_OID (&oid),s1,s2);
f=1;
}
else{
query = BCON_NEW ("_id", BCON_OID (&oid));
update = BCON_NEW("$set", "{", s1, s2, "}");
}
}
while(c!='}');
}
}
fclose(fp1);
bson_destroy (doc);
mongoc_collection_destroy (collection);
mongoc_client_destroy (client);
return 0;
}
I wish to import one record one document at a time into the monogdb.
I am compiling this program using gcc in fedora 21. There are no errors according to the compiler. But, when I run this program, it shows the following error:
2015/09/22 18:32:15.0303: [13623]: DEBUG: cluster: Client initialized in direct mode.
Segmentation fault (core dumped)
I want to import one record one document at a time into the monogdb. Please can you point out the mistake from this code? Thanks in advance. P.S This is my first question in StackOverflow. Please forgive any shortcomings.:)