개요
MongoDB deployment에 연결하려면 다음 두 가지가 필요합니다.
연결 문자열 이라고도 하는연결 URI는 Rust 운전자 에 연결할 MongoDB deployment 알려줍니다.
클라이언트 인스턴스- MongoDB deployment 에 대한 연결을 생성하고 해당 인스턴스에서 작업을 수행할 수 있습니다.
또한 이러한 구성 요소 중 하나를 사용하여 MongoDB 에 연결된 동안 Rust 운전자 작동하는 방식을 사용자 지정할 수 있습니다.
이 가이드 에서는 연결 string 을 만들고 Client 인스턴스 를 사용하여 MongoDB 에 연결하는 방법을 보여줍니다.
연결 URI
연결 문자열이라고도 하는 연결 URI는 드라이버에 MongoDB에 연결하는 방법과 연결된 동안 동작하는 방법을 알려줍니다.
표준 연결 string 에는 다음 구성 요소가 포함됩니다.
구성 요소 | 설명 |
|---|---|
| 필수 사항입니다. 표준 연결 형식의 문자열로 식별하는 접두사입니다. |
| 선택 사항. 인증 자격 자격 증명. 이를 포함하면 클라이언트 |
| 필수입니다. MongoDB 실행 호스트 이름 및 선택적 포트 번호입니다. 포트 번호를 포함하지 않으면 운전자 기본값 포트인 |
| 선택 사항. 연결 문자열 인증 자격 증명 포함되지만 |
| 선택 사항. 연결별 옵션을 쌍으로 지정하는 쿼리 |
연결 생성에 대한 자세한 string 내용은 MongoDB Server 설명서에서 연결 문자열 을 참조하세요.
MongoDB 클라이언트 만들기
클라이언트 는 연결을 관리하고 데이터베이스 명령을 실행합니다. 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 }; 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 형식은 여러 스레드 또는 비동기 작업에서 동시에 사용하기에 안전합니다.
API 문서
Rust 운전자 사용하여 Client 인스턴스 만드는 방법에 대해 자세히 학습 다음 API 설명서를 참조하세요.