我有一个小程序试图连接到mongodb并检查如果我希望接收对象它会收到多少个对象。在这种情况下,我甚至无法获得对象的数量,即使除了连接之外什么都不做,我也会对清理进行分段。
我使用GlobalInstance对象维护我的连接并在退出时为我清理。这个函数我从main调用。该计划尚未做任何其他事情。
有关我错误操作的哪些建议我提出了哪些建议?据推测,这与无法调用connection-> count()?
有关非常感谢任何指示。
int foo() {
mongo::client::Options options;
options.setSSLMode(mongo::client::Options::kSSLRequired);
mongo::client::GlobalInstance mongo_connection(options);
mongo::DBClientBase* connection;
if (!mongo_connection.status().isOK()) {
cout << "Mongo connection not established: "
<< mongo_connection.status() << endl;
}
try {
mongo::DBClientConnection c(true);
string error;
ostringstream mongo_url;
mongo_url << "mongodb://" << db_username << ":" << db_password << "@"
<< db_host << ":" << db_port << "/" << db_name;
const mongo::ConnectionString conn_string =
mongo::ConnectionString::parse(mongo_url.str(), error);
if (!conn_string.isValid()) {
cout << "Bad connection string: " << error << endl;
}
// Reality check. Passes.
cout << "user=" << conn_string.getUser() << endl;
cout << "pass=" << conn_string.getPassword() << endl;
cout << "db= " << conn_string.getDatabase() << endl;
const auto servers = conn_string.getServers();
for (const auto& server : servers) {
cout << "serv=" << server << endl;
}
cout << "type=" << conn_string.type() << endl;
// End reality check.
connection = conn_string.connect(error);
cout << "error says: " << error << endl; // Is empty.
cout << "conn=" << connection << endl; // Not zero.
std::cout << "connected ok" << std::endl;
cout << connection->getConnectionId() << endl; // Prints "1".
// This returns an error 13, not authorized, if I included it.
//cout << "count: " << connection->count("focus_groups") << endl;
} catch( const mongo::DBException &e ) {
std::cout << "caught " << e.what() << std::endl;
}
cout << "----------------------------------------------------------------------" << endl;
return 0;
}
/*
Note that I can do this in the mongo shell with no problem:
jeff@siegfried:~ $ mongo my_host:27017/my_db --ssl -u my_user -p
MongoDB shell version: 2.6.3
Enter password:
connecting to: my_host:27017/my_db
> db.focus_groups.count()
26
>
*/
我在64位ubuntu 15.04上运行并使用clang 3.6.0进行编译。通过apt-get安装Boost 1.55。我使用ssl支持从HEAD(861699d116627d63e1c914384a66e4e3ea7c23bc)的git编译和安装的传统mongo C ++驱动程序。
答案 0 :(得分:0)
这结果是几件事的汇合,但至少这两件事:
显示广泛的帖子here。