如何在 MongoDB C++ 中使用 DBClientBase 类插入文档司机?
我没有使用 DBClientConnection 类,而是使用 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)
中标志的用途,如提到的 此处
Instead of using DBClientConnection class, I am using DBClientBase class. I am successfully able to connect to the DB but not able to insert a document.
Here is how my code looks like-
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);
the compiler gives error request for member ‘insert’ in ‘conn’, which is of non-class type ‘mongo::DBClientBase*’
Is there an example somewhere on how to use DBClientBase class to insert documents?
Also, I cannot seem to find what is the use of flags in virtual void insert (const string &ns, BSONObj obj, int flags=0)
as mentioned here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
conn 是一个指向
DBClientBase
的指针,您应该使用->
来代替:conn is a pointer to a
DBClientBase
, you should use->
instead: