如何在 MongoDB C++ 中使用 DBClientBase 类插入文档司机?

发布于 2024-12-26 00:44:10 字数 1022 浏览 0 评论 0原文

我没有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

A君 2025-01-02 00:44:10

conn 是一个指向 DBClientBase指针,您应该使用 -> 来代替:

conn->insert("db.coll", p, 0);

conn is a pointer to a DBClientBase, you should use -> instead:

conn->insert("db.coll", p, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文