From 29e7267d3122848555d7c13549190115fa3a0d94 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Wed, 17 Jun 2026 01:31:08 +0800 Subject: [PATCH] fix(mongodb): set directConnection=true for single-host to bypass replica set discovery --- crates/dbx-core/src/db/mongo_driver.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/dbx-core/src/db/mongo_driver.rs b/crates/dbx-core/src/db/mongo_driver.rs index a1b8fed386..0c7e74e16f 100644 --- a/crates/dbx-core/src/db/mongo_driver.rs +++ b/crates/dbx-core/src/db/mongo_driver.rs @@ -31,6 +31,13 @@ pub async fn connect(url: &str, timeout: Duration, idle_timeout: Duration) -> Re if idle_timeout.as_secs() > 0 { options.max_idle_time = Some(idle_timeout); } + // For single-host connections, force direct connection to avoid replica + // set discovery. This is essential when connecting through a TCP proxy + // or NAT where the driver would otherwise receive internal IPs from + // the replica set handshake and fail to connect. + if !is_multi_host { + options.direct_connection = Some(true); + } Client::with_options(options).map_err(|e| format!("MongoDB connection failed: {e}")) }) .await