Fix MongoDB connection issues

This commit is contained in:
Nikolay Degterinsky 2023-10-09 14:44:02 +00:00
parent ca7796ba85
commit a1e56a3df1
3 changed files with 27 additions and 6 deletions

View File

@ -70,7 +70,7 @@ namespace MongoDB
Document::Ptr queryBuildInfo(Connection & connection) const; Document::Ptr queryBuildInfo(Connection & connection) const;
/// Queries server build info (all wire protocols) /// Queries server build info (all wire protocols)
Document::Ptr queryServerHello(Connection & connection) const; Document::Ptr queryServerHello(Connection & connection, bool old = false) const;
/// Queries hello response from server (all wire protocols) /// Queries hello response from server (all wire protocols)
Int64 count(Connection & connection, const std::string & collectionName) const; Int64 count(Connection & connection, const std::string & collectionName) const;

View File

@ -356,11 +356,19 @@ Document::Ptr Database::queryBuildInfo(Connection& connection) const
} }
Document::Ptr Database::queryServerHello(Connection& connection) const Document::Ptr Database::queryServerHello(Connection& connection, bool old) const
{ {
// hello can be issued on "config" system database // hello can be issued on "config" system database
Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createCommand(); Poco::SharedPtr<Poco::MongoDB::QueryRequest> request = createCommand();
request->selector().add("hello", 1);
// 'hello' command was previously called 'isMaster'
std::string command_name;
if (old)
command_name = "isMaster";
else
command_name = "hello";
request->selector().add(command_name, 1);
Poco::MongoDB::ResponseMessage response; Poco::MongoDB::ResponseMessage response;
connection.sendRequest(*request, response); connection.sendRequest(*request, response);

View File

@ -370,9 +370,22 @@ namespace
bool isMongoDBWireProtocolOld(Poco::MongoDB::Connection & connection_) bool isMongoDBWireProtocolOld(Poco::MongoDB::Connection & connection_)
{ {
Poco::MongoDB::Database db("config"); Poco::MongoDB::Database db("config");
Poco::MongoDB::Document::Ptr doc = db.queryServerHello(connection_); Poco::MongoDB::Document::Ptr doc = db.queryServerHello(connection_, false);
if (doc->exists("maxWireVersion"))
{
auto wire_version = doc->getInteger("maxWireVersion"); auto wire_version = doc->getInteger("maxWireVersion");
return wire_version < Poco::MongoDB::Database::WireVersion::VER_36; return wire_version < Poco::MongoDB::Database::WireVersion::VER_36;
}
doc = db.queryServerHello(connection_, true);
if (doc->exists("maxWireVersion"))
{
auto wire_version = doc->getInteger("maxWireVersion");
return wire_version < Poco::MongoDB::Database::WireVersion::VER_36;
}
return false;
} }