0

I am trying to retrieve an object out of my MongoDB instance. I am using the JsonCPP library.

Currently, what I am doing is:

system(("mongo --host " + host_name + " --port " +  std::to_string(port) + " " + database_name + " --eval 'db." + collection_name + ".find({},{_id:0})'  | tee -a return_from_db.json").c_str());

And parsing it later on using:

Json::Value json_object;
Json::Reader jsonreader.parse(ifstream_from_return_from_db_json, json_object, false);

As soon as I am not suppressing the _id field in my query, I'll get null values everywhere. The reason for this is as follows:

{
    "_id": ObjectId("any_id")
}
  • The object ID is not in double quotes.

Now my question: How can I extract the ID of a document using the jsoncpp library? Can I change something in the settings of my MongoDB instance to get syntactically correct id key-value mappings? I know, there is the MongoDB driver for CPP, but I cannot use it (for a couple of reasons...). Any help appreciated.

Karlo Kraljic
  • 173
  • 2
  • 11
  • What you are trying to do is doomed to endless frustration. Just use a driver. You say you can't use one of the C++ drivers? Why not? Even then, why not just use the C driver? – acm Aug 16 '16 at 23:12

1 Answers1

0

The MongoDB shell only looks like JSON. It provides a custom, extended form to preserve type information not available in pure JSON, so your approach won't work for any document projection that includes such types.

You can read more about it in the Extended JSON docs (see "Shell mode").

If you can't use a driver, you might want to explore a third party REST interface instead.

xdg
  • 2,975
  • 19
  • 17