0

I have a buffer i.e a char pointer which has some jSON data itself ,so I want to insert this data into Mongodb.

My approach is,

           char *buff=NULL;
           {buff has some JSON data}

           mongo::BSONObj *obj_data= new mongo::BSONObj(buff) ;
           conn.insert( "mydb.testcollect",obj_data,0);

I am getting this message,

error: no matching function for call to ‘mongo::DBClientConnection::insert(const char [17], mongo::BSONObj*&, int)’

What will be the right approach .plz help.

2 Answers 2

1

Assuming you are using the latest version of the legacy driver, you should include db/json.h, and then write the following:

try {
    mongo::BSONObj obj = mongo::fromjson(buff);
}
catch (const mongo::MsgAssertionException& exception) {
    // Handle parsing failure.
}
conn.insert("mydb.testcollect", obj);
std::string error = conn.getLastError();
if (!error.empty()) {
    // Handle remote error.
}

If you're starting a new project, then strongly consider using the mongocxx driver (versions 3.0.x and greater of C++ driver series) instead of the legacy driver. The improved API of mongocxx will make writing C++ client code for MongoDB much more intuitive.

Sign up to request clarification or add additional context in comments.

Comments

0

Try:

       mongo::BSONObj obj_data(buff) ;
       conn.insert( "mydb.testcollect", obj_data,0);

By the way, you are using outdated driver version. Consider switching to latest one.

3 Comments

yes now am not getting error at compile time,but at run time am getting this error,"errorThu Sep 29 15:13:23 Assertion: 10334:Invalid BSONObj size: 1919230587
@RinkuBuragohain well that is next question. I am not sure you can just feed JSON string to BSONObj constructor. You should check documentation but it is rather vague "Construct a BSONObj from data in the proper format.": api.mongodb.com/cxx-26compat/legacy-0.0-26compat-2.6.0-rc0/…
@RinkuBuragohain I would suggest to switching to latest version of driver, it has better structure and docs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.