Docs Menu
Docs Home
/ / /
Go 드라이버
/

문서 수 계산

EstimatedDocumentCount() 메서드를 사용하면 collection의 문서에 대한 대략적인 근사치를 얻고 CountDocuments() 메서드를 사용하여 collection의 정확한 문서 수를 얻을 수 있습니다.

이 예제를 실행하는 방법을 알아보려면 사용 예제 를 읽어보세요.

다음 예시에서는 movies 컬렉션에서 다음을 수행합니다.

  • 컬렉션의 문서 수 추정

  • countries에 'China'가 포함된 문서의 수를 계산합니다.

coll := client.Database("sample_mflix").Collection("movies")
// Specifies a filter to match documents where the "countries" array
// includes a value of "China"
filter := bson.D{{"countries", "China"}}
// Retrieves and prints the estimated number of documents in the collection
estCount, estCountErr := coll.EstimatedDocumentCount(context.TODO())
if estCountErr != nil {
panic(estCountErr)
}
// Retrieves and prints the number of documents in the collection
// that match the filter
count, err := coll.CountDocuments(context.TODO(), filter)
if err != nil {
panic(err)
}

실행 가능한 예시 보기

전체 예시를 실행하면 다음과 같은 내용이 표시됩니다.

  • movies 컬렉션에 약 23541개의 문서가 있습니다.

  • movies 컬렉션에 countries 필드에 '중국'이 포함된 303 문서가 있습니다.

참고

정확한 문서 수는 데이터 세트에 따라 다를 수 있습니다.

문서 수 계산에 대해 자세히 알아보려면 문서 수 계산을 참조하세요.

돌아가기

데이터 변경 사항 모니터링