一个基于REACT-CPP的库,使用C++编写的异步mogo访问库。通过使用lambda表达式和回调函数的形式来返回查询结果。
这个库的工作原理与普通的mogoC++库基本相同,只是所有函数接受一个lambda参数,当结果可用(或失败)时将调用该参数。
示例代码
React::Mogo::Coectio mogo("mogodb.example.org", [](React::Mogo::Coectio *coectio, cost char *error) { // if o error occured, we will receive a ull poiter if (!error) std::cout << "Coected successfully" << std::edl; // otherwise, we will get a descriptio of what exactly wet wrog else std::cout << "Coectio error: " << error << std::edl;});/** * The mogo object will be created immediately, eve though the coectio * might ot have bee established. It is safe, however, to immediately ru * the ext commad o the object. They will be executed after a coectio * was established. Should the library fail to coect, all registered calls * will receive a error. */// build a query to fid a specific documetVariat::Value query;query["_id"] = "documetid";// retrieve the documetmogo.query("database.collectio", std::move(query)).oSuccess([](Variat::Value&& result) { // sice we search for a exact id, we will get a maximum of oe result // however, this result will always be a array if (result.size() == 0) { std::cout << "Could ot fid ay documet with that ID" << std::edl; retur; } // assume that the documet has a strig field amed 'firstame' std::strig firstame = result[0]["firstame"];}).oError([](cost char *error) { std::cout << "Somethig wet wrog queryig: " << error << std::edl;});
评论