如何在MongoDB C++驱动程序中使用DBClientBase类插入文档?
问题描述:
而不是使用DBClientConnection类,我正在使用DBClientBase类。我成功地能够连接到数据库但无法插入文档。如何在MongoDB C++驱动程序中使用DBClientBase类插入文档?
这里是我的代码看起来喜欢 -
DBClientBase *conn = NULL;
string err_msg;
ConnectionString cs = ConnectionString::parse(connString, err_msg);
if (!cs.isValid()) {
throw "bad: " + err_msg;
}
try {
conn = cs.connect(err_msg);
} catch (DBException &e) {
cout << "caught " << err_msg << endl;
return 1;
}
if (!conn){
cout<<"Unable to connect to DB"<<endl;
return 1;
}
BSONObjBuilder b;
b.append("name", "Joe");
b.append("age", 33);
BSONObj p = b.obj();
conn.insert("db.coll",p,0);
的编译器会发出错误request for member ‘insert’ in ‘conn’, which is of non-class type ‘mongo::DBClientBase*’
有一个例子某处如何使用DBClientBase类插入文件?
而且,我似乎无法找出什么是在virtual void insert (const string &ns, BSONObj obj, int flags=0)
使用标志提到here
答
conn是一个指针到DBClientBase
,你应该使用->
代替:
conn->insert("db.coll", p, 0);