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
/ /

CRUD 작업 구성

이 가이드 에서는 Rust 운전자 에서 읽기 및 쓰기 (write) 작업을 구성하는 방법을 학습 수 있습니다.

읽기 설정( read preference)을 설정하여 드라이버가 읽기 작업을 라우팅하는 방법을 제어할 수 있습니다. 또한 읽기 고려 및 쓰기 고려를 설정하여 드라이버가 복제본 세트에 대한 읽기쓰기 작업의 승인을 기다리는 방법에 대한 옵션을 제어할 수도 있습니다.

기본값 으로 데이터베이스는 인스턴스 에서 이러한 설정을 상속하고 컬렉션은 데이터베이스 에서 설정을 상속합니다. 그러나 Client database_with_options() 또는 collection_with_options() 메서드를 사용하여 데이터베이스 또는 컬렉션 에 대한 사용자 지정 읽기 또는 쓰기 (write) 설정을 설정하다 수 있습니다. 이 메서드는 Database Collection 지정된 설정으로 새 또는 인스턴스 만듭니다.

읽기 또는 쓰기 (write) 설정을 구성하려면 ReadConcern, WriteConcern 또는SelectionCriteria 유형의 인스턴스 만들어 옵션 빌더에 전달합니다.

읽기 및 쓰기 설정에 대해 자세히 알아보려면 MongoDB Server 매뉴얼에서 다음 가이드를 참조하세요.

다음 섹션에서는 클라이언트, 데이터베이스 및 컬렉션 수준에서 읽기 및 쓰기 (write) 설정을 구성하는 방법을 보여줍니다.

이 예시 객체 Client::with_options() 메서드에 전달하여 인스턴스 의 읽기 설정 (read preference), 읽기 고려 (read concern) 및 쓰기 고려 (write concern) 설정하다 방법을 Client ClientOptions 보여줍니다. 이 코드는 다음 설정을 구성합니다.

  • Secondary 읽기 설정 (read preference) : 읽기 작업은 세컨더리 복제본 세트 멤버에서 데이터를 조회 합니다.

  • local 읽기 고려 (read concern): 읽기 작업은 데이터가 대부분의 복제본 세트 멤버에 기록되었음을 보장하지 않고 인스턴스의 가장 최근 데이터를 반환합니다.

  • Majority 쓰기 고려 (write concern): 모든 복제본 세트 멤버의 대다수가 쓰기 (write) 작업을 승인해야 합니다.

let read_preference = ReadPreference::Secondary {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::local();
let write_concern = WriteConcern::builder().w(Acknowledgment::Majority).build();
let client_options = ClientOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let client = Client::with_options(client_options)?;

또는 다음 예시 와 같이 연결 URI에서 읽기 및 쓰기 (write) 설정을 지정한 다음 URI를 Client::with_uri_str() 생성자에 전달할 수 있습니다.

let uri = "<connection string>/?readPreference=secondary&readConcernLevel=local&w=majority";
let client = Client::with_uri_str(uri).await?;

이 예시 객체 test_database DatabaseOptions database_with_options() 메서드에 전달하여 이름이 인 데이터베이스 의 읽기 설정 (read preference), 읽기 고려 (read concern) 및 쓰기 고려 (write concern) 설정하다 방법을 보여 줍니다. 이 코드는 다음 설정을 구성합니다.

  • PrimaryPreferred 읽기 설정 (read preference) : 읽기 작업은 프라이머리 복제본 세트 멤버에서 데이터를 조회 하거나, 프라이머리 멤버를 사용할 수 없는 경우 세컨더리 멤버에서 데이터를 검색합니다.

  • available 읽기 고려 (read concern): 읽기 작업은 데이터가 대부분의 복제본 세트 멤버에 기록되었음을 보장하지 않고 인스턴스의 가장 최근 데이터를 반환합니다.

  • majority 쓰기 고려 (write concern): 모든 복제본 세트 멤버의 대다수가 쓰기 (write) 작업을 승인해야 합니다.

let read_preference = ReadPreference::PrimaryPreferred {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::available();
let write_concern = WriteConcern::majority();
let database_options = DatabaseOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let database = client.database_with_options("test_database", database_options);

이 예시 객체 test_collection CollectionOptions collection_with_options() 메서드에 전달하여 이름이 인 컬렉션 의 읽기 설정 (read preference), 읽기 고려 (read concern) 및 쓰기 고려 (write concern) 설정하다 방법을 보여줍니다. 이 코드는 다음 설정을 구성합니다.

  • SecondaryPreferred 읽기 설정 (read preference) : 읽기 작업은 세컨더리 복제본 세트 멤버 또는 사용 가능한 세컨더리 멤버가 없는 경우 프라이머리 멤버에서 데이터를 조회.

  • available 읽기 고려 (read concern): 읽기 작업은 데이터가 대부분의 복제본 세트 멤버에 기록되었음을 보장하지 않고 인스턴스의 가장 최근 데이터를 반환합니다.

  • w: 0 쓰기 고려 (write concern): 클라이언트 쓰기 (write) 작업에 대한 확인을 요청 하지 않습니다.

let read_preference = ReadPreference::SecondaryPreferred {
options: Default::default(),
};
let selection_criteria = SelectionCriteria::ReadPreference(read_preference);
let read_concern = ReadConcern::available();
let write_concern = WriteConcern::builder().w(0.into()).build();
let collection_options = CollectionOptions::builder()
.selection_criteria(selection_criteria)
.read_concern(read_concern)
.write_concern(write_concern)
.build();
let collection = database.collection_with_options("test_collection", collection_options);

Rust 운전자 네트워크 또는 서버 오류로 인해 특정 읽기 및 쓰기 (write) 작업이 실패할 경우 자동으로 한 번만 다시 시도합니다.

ClientOptions 인스턴스 빌드할 때 retry_reads 또는 retry_writes 옵션을 false 로 설정하여 재시도 가능 읽기 또는 재시도 가능 쓰기를 명시적으로 비활성화할 수 있습니다. 다음 예시 클라이언트 에 대해 재시도 가능 읽기 및 쓰기를 비활성화합니다.

let client_options = ClientOptions::builder()
.retry_reads(false)
.retry_writes(false)
.build();
let client = Client::with_options(client_options)?;

지원되는 재시도 가능 읽기 작업에 대해 자세히 학습 MongoDB Server 매뉴얼의 재시도 가능 읽기를 참조하세요. 지원되는 재시도 가능 쓰기 (write) 작업에 대해 자세히 학습 MongoDB Server 매뉴얼의 재시도 가능 쓰기 참조하세요.

데이터 정렬 을 지정하여 읽기 및 쓰기 (write) 작업의 동작을 수정할 수 있습니다. 데이터 정렬은 대소문자 및 악센트 표시와 같이 문자열 비교를 위한 언어별 규칙 설정하다 입니다.

데이터 정렬에 대한 자세한 내용과 코드 예제를 보려면 데이터 정렬 가이드 참조하세요.

이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.

돌아가기

복합 작업

이 페이지의 내용