Explain(executionStats) in c++

How can i execute this command but in cpp
db.name_collections.find({x: 8}).explain('executionStats')

I would like to do something like std::cout << explain

Hi @Leno_N_A and welcome to the MongoDB community forum!!

The explain() command can be sent with the runCommand method.
The below code snippet might be helpful for you.

using namespace bsoncxx::builder::basic;
auto reply = db.run_command(make_document(kvp("explain", make_document(kvp("find", "coll"), kvp("filter", make_document(kvp("foo", "bar")))))));
auto jsonStr = bsoncxx::to_json(reply.view());
std::cout << jsonStr << std::endl;

Let us know if you have any further queries.

Best Regards


This topic was automatically closed after 60 days. New replies are no longer allowed.