1

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.:)

Abin
  • 113
  • 1
  • 1
  • 8
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Sep 22 '15 at 13:28
  • Note also that `main()` is not a valid signature for `main`. Please use prototype-style with the correct arguments/result types. – too honest for this site Sep 22 '15 at 13:28
  • There is no range s1 & s2 at all – Totonga Sep 23 '15 at 06:47

1 Answers1

0

Probably you can use:

const char* fileContentAsUtf8;
bson_t* query = bson_new_from_json(reinterpret_cast<const uint8_t*>(fileContentAsUtf8), -1, &error);

and afterwards use the bson methods to extract the necessary information instead of writing your own json parser.

Totonga
  • 4,236
  • 2
  • 25
  • 31