개요
이 가이드 에서는 Rust 운전자 사용하여 컬렉션 에 있는 문서 수의 정확한 예상 개수를 조회 방법을 학습 수 있습니다. count_documents() 메서드는 쿼리 필터하다 와 일치하거나 컬렉션 에 존재하는 정확한 문서 수를 반환하고, estimated_document_count() 메서드는 컬렉션 의 예상 문서 수를 반환합니다.
샘플 데이터
이 가이드 의 예제에서는 restaurants sample_restaurants Atlas 샘플 데이터 세트의 데이터베이스 에 있는 컬렉션 사용합니다. Rust 애플리케이션 에서 이 컬렉션 액세스 하려면 Client Atlas cluster 에 연결하는 를 만들고 my_coll 변수에 다음 값을 할당합니다.
Asynchronous 또는 Synchronous 탭을 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let uri = "<connection string>"; let client = Client::with_uri_str(uri).await?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
let uri = "<connection string>"; let client = Client::with_uri_str(uri)?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
무료 MongoDB Atlas cluster 생성하고 샘플 데이터 세트를 로드하는 방법을 학습 보려면 Rust 드라이버 시작하기 가이드 참조하세요.
이 가이드의 예시에서는 restaurants collection의 문서에 대한 모델로 다음 Restaurant 구조체를 사용합니다.
struct Restaurant { name: String, cuisine: String, }
정확한 개수 조회
컬렉션 에 있는 문서 수를 계산하려면 count_documents() 메서드를 사용합니다. 특정 검색 기준과 일치하는 문서 수를 계산하려면 쿼리 필터하다 문서 count_documents() 메서드에 전달합니다.
쿼리 지정에 대해 자세히 알아보려면 쿼리 지정 가이드를 참조하세요.
모든 문서 계산
컬렉션 에 있는 모든 문서의 개수를 반환하려면 다음 예시 와 같이 빈 필터하다 문서 count_documents() 메서드에 전달합니다. Asynchronous 또는 Synchronous 탭 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let ct = my_coll.count_documents().await?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
let ct = my_coll.count_documents().run()?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
특정 문서 수 계산
특정 검색 기준과 일치하는 문서 수를 반환하려면 쿼리 필터하다 문서 를 count_documents() 메서드에 전달합니다.
다음 예시 name 필드 의 값에 "Sunset" 문자열이 포함된 문서 수를 계산합니다. Asynchronous 또는 Synchronous 탭 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .await?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .run()?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
카운트 동작 사용자 지정
await 또는 run() 메서드 호출 전에 옵션 메서드를 count_documents() 호출에 연결하여 count_documents() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 카운트 작업을 사용자 지정하기 위해 설정하다 수 있는 옵션에 대해 설명합니다.
옵션 | 설명 |
|---|---|
| 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 number of documents to count. This value must be a positive integer. Type: u64 |
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The read concern to use for the operation. Type: ReadConcern |
| The number of documents to skip before counting documents. Type: u64 |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
예상 개수 조회
await 또는 run() 메서드 호출 전에 estimated_document_count() 메서드를 호출하여 컬렉션 에 있는 문서 수의 추정치를 조회 할 수 있습니다. 이 메서드는 컬렉션 메타데이터 기반으로 문서의 양을 추정하며, 이는 정확한 개수를 계산하는 것보다 빠를 수 있습니다.
다음 예시 에서는 컬렉션 의 문서 수를 추정합니다. Asynchronous 또는 Synchronous 탭 선택하여 각 런타임에 해당하는 코드를 확인합니다.
let ct = my_coll.estimated_document_count().await?; println!("Number of documents: {}", ct);
Number of documents: 25216
let ct = my_coll.estimated_document_count().run()?; println!("Number of documents: {}", ct);
Number of documents: 25216
예상 카운트 동작 사용자 지정
옵션 메서드를 estimated_document_count() 호출에 연결하여 estimated_document_count() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 예상 카운트 작업을 사용자 지정하기 위해 설정하다 수 있는 옵션에 대해 설명합니다.
옵션 | 설명 |
|---|---|
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The comment to attach to the operation. Type: Bson |
| The read concern to use for the operation. Type: ReadConcern |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
API 문서
이 가이드에서 사용되는 메서드 또는 유형에 대해 자세히 알아보려면 다음 API 설명서를 참조하세요.