Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /

MongoDB 클라이언트 만들기

MongoDB deployment에 연결하려면 다음 두 가지가 필요합니다.

  • 연결 문자열 이라고도 하는연결 URI는 Rust 운전자 에 연결할 MongoDB deployment 알려줍니다.

  • 클라이언트 인스턴스- MongoDB deployment 에 대한 연결을 생성하고 해당 인스턴스에서 작업을 수행할 수 있습니다.

또한 이러한 구성 요소 중 하나를 사용하여 MongoDB 에 연결된 동안 Rust 운전자 작동하는 방식을 사용자 지정할 수 있습니다.

이 가이드 에서는 연결 string 을 만들고 Client 인스턴스 를 사용하여 MongoDB 에 연결하는 방법을 보여줍니다.

연결 문자열이라고도 하는 연결 URI는 드라이버에 MongoDB에 연결하는 방법과 연결된 동안 동작하는 방법을 알려줍니다.

표준 연결 string 에는 다음 구성 요소가 포함됩니다.

구성 요소
설명

mongodb://

필수 사항입니다. 표준 연결 형식의 문자열로 식별하는 접두사입니다.

<db_username>:<db_password>

선택 사항. 인증 자격 자격 증명. 이를 포함하면 클라이언트 authSource에 지정된 데이터베이스 에 대해 사용자를 인증합니다. 연결 옵션에 대한 자세한 내용은 인증 authSource 메커니즘 가이드 참조하세요.

hostname[:port]

필수입니다. MongoDB 실행 호스트 이름 및 선택적 포트 번호입니다. 포트 번호를 포함하지 않으면 운전자 기본값 포트인 27017를 사용합니다.

/defaultauthdb

선택 사항. 연결 문자열 인증 자격 증명 포함되지만 authSource 옵션은 포함되지 않는 경우 사용할 인증 데이터베이스 입니다.

?<options>

선택 사항. 연결별 옵션을 쌍으로 지정하는 쿼리 <name>=<value> 문자열입니다. 이러한 옵션에 대한 전체 설명은 연결 옵션 지정 가이드 참조하세요.

연결 생성에 대한 자세한 string 내용은 MongoDB Server 설명서에서 연결 문자열 을 참조하세요.

클라이언트 는 연결을 관리하고 데이터베이스 명령을 실행합니다. Client 인스턴스 만들려면 다음 예시 와 같이 연결 문자열 Client::with_uri_str() 메서드에 전달합니다.

let client = Client::with_uri_str(uri)?;

클라이언트 생성할 때 ClientOptions 객체 with_options() 메서드에 전달하여 연결 옵션을 지정할 수 있습니다.

연결 문자열 연결 문자열 연결 옵션을 지정할 parse() 수도 ClientOptions 있습니다.

다음 코드 예제는 클라이언트 생성하고, Stable API 연결 옵션을 지정하고, MongoDB 에 연결하고, 연결이 성공적인 했는지 확인하는 방법을 보여줍니다. 해당 코드 예시 보려면 아래에서 비동기 API 또는 동기 API 탭 선택하세요.

use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, Client };
#[tokio::main]
async fn main() -> mongodb::error::Result<()> {
// Replace the placeholder with your Atlas connection string
let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).await?;
// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);
// Create a new client and connect to the server
let client = Client::with_options(client_options)?;
// Send a ping to confirm a successful connection
client.database("admin").run_command(doc! { "ping": 1 }).await?;
println!("Pinged your deployment. You successfully connected to MongoDB!");
Ok(())
}
use mongodb::{ bson::doc, options::{ ClientOptions, ServerApi, ServerApiVersion }, sync::Client };
fn main() -> mongodb::error::Result<()> {
// Replace the placeholder with your Atlas connection string
let uri = "<connection string>";
let mut client_options = ClientOptions::parse(uri).run()?;
// Set the server_api field of the client_options object to Stable API version 1
let server_api = ServerApi::builder().version(ServerApiVersion::V1).build();
client_options.server_api = Some(server_api);
// Create a new client and connect to the server
let client = Client::with_options(client_options)?;
// Send a ping to confirm a successful connection
client.database("admin").run_command(doc! { "ping": 1 }).run()?;
println!("Pinged your deployment. You successfully connected to MongoDB!");
Ok(())
}

사용 가능한 연결 옵션에 대한 자세한 내용은 연결 옵션 지정 가이드 참조하세요.

클라이언트 재사용

세션과 작업에서 클라이언트를 재사용하여 성능을 향상시킬 수 있습니다. 매번 새 인스턴스를 만드는 대신 동일한 Client 인스턴스를 사용하여 여러 작업을 수행할 수 있습니다. Client 형식은 여러 스레드 또는 비동기 작업에서 동시에 사용하기에 안전합니다.

Rust 운전자 사용하여 Client 인스턴스 만드는 방법에 대해 자세히 학습 다음 API 설명서를 참조하세요.

돌아가기

시작하기

이 페이지의 내용