개요
이 가이드 에서는 Rust 운전자 사용하여 컬렉션 전체에서 지정된 필드 의 고유 값을 조회 방법을 학습 수 있습니다.
컬렉션 내에서 문서에는 단일 필드 에 대해 서로 다른 값이 포함될 수 있습니다. 예시 들어 restaurants 컬렉션 의 한 문서 borough 값이 "Manhattan"이고 다른 문서의 borough 값은 "Queens"입니다. Rust 운전자 사용하여 restaurants 컬렉션 의 모든 문서에서 "Manhattan", "Queens" 및 borough 필드 의 기타 모든 고유 값을 조회 할 수 있습니다.
샘플 데이터
이 가이드 의 예제에서는 Atlas 샘플 데이터 세트의 sample_restaurants 데이터베이스 에 있는 restaurants 컬렉션 사용합니다. 무료 MongoDB Atlas cluster 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드를 참조하세요.
restaurants 컬렉션 의 문서에 Document 유형 또는 사용자 지정 데이터 유형 의 인스턴스로 액세스 할 수 있습니다. 컬렉션의 데이터를 나타내는 데이터 유형 지정하려면 <T> 유형 매개변수를 다음 값 중 하나로 바꿉니다.
<Document>: 컬렉션 문서를 BSON 문서로 나타냅니다.<Restaurant>: 컬렉션 문서를 코드 예제에 정의된Restaurant구조체의 인스턴스로 나타냅니다.
Retrieve Distinct Values
지정된 필드 의 고유 값을 조회 하려면 인스턴스 에서 distinct() 메서드를 Collection 호출하고 고유 값을 찾으려는 필드 의 이름을 전달합니다.
메서드는 distinct() 고유 값 목록을 Vec<Bson> 객체,Bson 값으로 구성된 벡터로 반환합니다.
컬렉션 전체에서 값 검색
다음 예시 restaurants 컬렉션 에 있는 borough 필드 의 고유 값을 검색합니다.
Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let boroughs = my_coll.distinct("borough", None).await?; println!("List of field values for 'borough':"); for b in boroughs { println!("{:?}", b); }
List of field values for 'borough': String("Bronx") String("Brooklyn") String("Manhattan") String("Missing") String("Queens") String("Staten Island")
let boroughs = my_coll.distinct("borough", None).run()?; println!("List of field values for 'borough':"); for b in boroughs { println!("{:?}", b); }
List of field values for 'borough': String("Bronx") String("Brooklyn") String("Manhattan") String("Missing") String("Queens") String("Staten Island")
이 연산은 각각의 고유한 borough 필드 값을 저장하는 벡터를 반환합니다. 여러 문서의 borough 필드 에 동일한 값이 있더라도 각 값은 결과에 한 번만 표시됩니다.
지정된 문서에서 값 검색
메서드에 쿼리 필터하다 distinct() 제공하여 컬렉션 의 문서 하위 집합에서 고유 필드 값을 찾을 수 있습니다. 쿼리 필터하다 는 작업에서 문서를 일치시키는 데 사용되는 검색 기준을 지정하는 표현식 입니다. 예시 들어 distinct() 메서드를 사용하여 일치하는 문서의 하위 집합에서만 고유 필드 값을 찾을 수 있습니다.
팁
쿼리 필터하다 만드는 방법에 대해 자세히 학습 쿼리 지정 가이드 참조하세요.
다음 예시 cuisine 필드 값이 "Turkish"인 모든 문서에 대해 borough 필드 의 고유 값을 검색합니다.
Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let filter = doc! { "cuisine": "Turkish" }; let boroughs = my_coll.distinct("borough", filter).await?; println!("List of field values for 'borough':"); for b in boroughs { println!("{:?}", b); }
List of field values for 'borough': String("Brooklyn") String("Manhattan") String("Queens") String("Staten Island")
let filter = doc! { "cuisine": "Turkish" }; let boroughs = my_coll.distinct("borough", filter).run()?; println!("List of field values for 'borough':"); for b in boroughs { println!("{:?}", b); }
List of field values for 'borough': String("Brooklyn") String("Manhattan") String("Queens") String("Staten Island")
고유 동작 수정
await 또는 run() 메서드 호출 전에 하나 이상의 옵션 메서드를 count_documents() 호출에 연결하여 distinct() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 고유 작업을 사용자 지정하기 위해 설정하다 수 있는 옵션에 대해 설명합니다.
옵션 | 설명 |
|---|---|
| The collation to use for the operation. Type: Collation |
| The index to use for the operation. Type: Hint |
| The comment to attach to the operation. Type: Bson |
| The maximum amount of time that the operation can run. Type: Duration |
| The read concern to use for the operation. Type: ReadConcern |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
API 문서
메서드에 대해 distinct() 자세히 학습 API 설명서를 참조하세요.