私は、mongoDBレプリカセット操作を実装するクラスを含むdllを構築しました。ここにクラスの要約があります。DLLのクラスのC++レガシードライバmongoDBレプリカ
#include "mongo/client/dbclient.h"
mongoimp::mongoimp() {
mongo::client::initialize();
}
mongoimp::~mongoimp() {
mongo::client::shutdown();
}
int mongoimp::queryTRecords() {
string errmsg;
vector<mongo::HostAndPort> hosts = { mongo::HostAndPort("xx-a0.yyyy.com:xxxxx"), mongo::HostAndPort("xx-a1.yyyy.com:xxxxx") };
static mongo::DBClientReplicaSet con("xx", hosts, 0);
con.connect();
con.auth("dbname", "username", "password", errmsg);
auto_ptr<DBClientCursor> cursor = con.query("dbname.t", BSONObj());
BSONObj response;
con.logout("xx", response);
if (cursor->more()) {
BSONObj recordnm = cursor->nextSafe();
return(recordnm.getIntField("lastid"));
} else return(-1);
}
上記のコードは動作しています。しかし、ここに私の質問があります:
1)上記の設定では、私は通常のmongoDB操作をdllで行うことができますが、私のアプリケーションは常にmongoDBデータをリアルタイムで更新する必要があります(最大数百秒)。データを更新するときにエラーが発生しました(有効なレプリカセットインスタンスサーバーが見つかりません)。
2)サーバーのみがmongoDBデータベースと通信する必要があります。ですから、基本的にはデータベースに接続するだけです。ですから、mongo :: DBClientReplicaSet conを静的なグローバル変数として宣言し、それをクラス構築関数で接続したいと思います。しかし、私はそれをすることができないようだった。私のアプリケーションはまったく動かすことができません。それで、私は常に次のエラーを取得します。
アサーションに失敗しました:!PX = 0、ファイルはC:\ブースト\は\ブースト-1_62 \ブースト/ smart_ptr/scoped_ptr.hppが含まれ、ライン105
を誰もが問題を解決する方法を知っていますか? 。。
以下は、私が試したコードです:
3)最後の質問
static mongo::DBClientReplicaSet con("xx", { mongo::HostAndPort("xx-a0.yyyy.com:xxxxx"), mongo::HostAndPort("xx-a1.yyyy.com:xxxxx") }, 0);
mongoimp::mongoimp() {
mongo::client::initialize();
string errmsg;
con.connect();
con.auth("dbname", "username", "password", errmsg);
}
mongoimp::~mongoimp() {
BSONObj response;
con.logout("xx", response);
mongo::client::shutdown();
}
int mongoimp::queryTRecords() {
auto_ptr<DBClientCursor> cursor = con.query("dbname.t", BSONObj());
if (cursor->more()) {
BSONObj recordnm = cursor->nextSafe();
return(recordnm.getIntField("lastid"));
} else return(-1);
}
が、私はモンゴ/クライアント/ dbclient_rs.h」ファイルがさらにreplicaSetのためにそこにある気づいた。しかし、それは私がそれで、それを使用することができないように見えたが、私はinitialize()とauto_ptr cursorにエラーが発生しています。どのようにしてファイルを使用し、レプリカセットの機能を最大限に活用できますか?「dbclient_rs.h」を使用することができる場合はどうすればよいですか?その場合のデータは?
ありがとうございます!