문서 메뉴
문서 홈
/ / /
C#/.NET
/ / /

문서 수 계산

이 페이지의 내용

  • 개요
  • 정확한 카운트
  • 예상 개수
  • 집계
  • 추가 정보

이 가이드에서는 컬렉션의 문서 수에 대한 개의 정확한 예상 개수를 얻는 방법에 대해 알아볼 수 있습니다.

이 가이드의 예에서는 students 컬렉션의 다음 문서를 사용합니다.

{ "_id": 1, "name": "Jonathon Howard ", "finalGrade": 87.5 }
{ "_id": 2, "name": "Keisha Freeman", "finalGrade": 12.3 }
{ "_id": 3, "name": "Wei Zhang", "finalGrade": 99.0 }
{ "_id": 4, "name": "Juan Gonzalez", "finalGrade": 85.5 }
{ "_id": 5, "name": "Erik Trout", "finalGrade": 72.3 }
{ "_id": 6, "name": "Demarcus Smith", "finalGrade": 88.8 }

다음 Student 클래스는 아래 컬렉션에 있는 문서를 모델링합니다.

public class Student {
public int Id { get; set; }
public string Name { get; set; }
public double FinalGrade { get; set; }
}

참고

students 컬렉션의 문서는 카멜 케이스 명명 규칙을 사용합니다. 이 가이드의 예제에서는 ConventionPack 사용하여 컬렉션의 필드를 파스칼식 대/소문자로 역직렬화하고 Student 클래스의 속성에 매핑합니다.

사용자 지정 직렬화에 대해 자세히 알아보려면 사용자 지정 직렬화를 참조하세요.

쿼리 필터와 일치하는 문서 수를 계산하려면 CountDocuments() 메서드를 사용하세요. 빈 쿼리 필터를 전달하는 경우 이 메서드는 컬렉션에 있는 총 문서 수를 반환합니다.

다음 예에서는 finalGrade80보다 작은 문서 수를 계산합니다.

var filter = Builders<Student>.Filter.Lt(s => s.FinalGrade, 80.0);
var count = _myColl.CountDocuments(filter);
Console.WriteLine("Number of documents with a final grade less than 80: " + count);

CountOptions 유형을 매개 변수로 전달하여 CountDocuments()의 동작을 수정할 수 있습니다. 옵션을 지정하지 않으면 드라이버는 기본값을 사용합니다.

CountOptions 객체에서 다음 속성을 설정할 수 있습니다.

속성
설명
Collation
The type of language collation to use when sorting results.
Default: nil
Hint
The index to use to scan for documents to count.
Default: nil
Limit
The maximum number of documents to count.
Default: 0
MaxTime
The maximum amount of time that the query can run on the server.
Default: nil
Skip
The number of documents to skip before counting.
Default: 0

CountDocuments()를 사용하여 컬렉션의 총 문서 수를 반환하면 MongoDB는 컬렉션 스캔을 수행합니다. _id 필드에 기본 제공되는 인덱스를 활용에 대한 힌트를 참고하여 컬렉션 스캔을 피하고 이 메서드의 성능을 향상시킬 수 있습니다. 빈 쿼리 매개 변수를 사용하여 CountDocuments()를 호출할 때만 이 방법을 사용합니다.

var filter = Builders<Student>.Filter.Empty;
CountOptions opts = new CountOptions(){Hint = "_id_"};
var count = collection.CountDocuments(filter, opts);

컬렉션의 총 문서 수를 추정하려면 EstimatedDocumentCount() 메서드를 사용합니다.

참고

EstimatedDocumentCount() 메서드는 전체 컬렉션을 스캔하는 대신 컬렉션의 메타데이터를 사용하기 때문에 CountDocuments() 메서드보다 빠릅니다.

EstimatedDocumentCountOptions 유형을 매개 변수로 전달하여 EstimatedDocumentCount()의 동작을 수정할 수 있습니다. 옵션을 지정하지 않으면 드라이버는 기본값을 사용합니다.

EstimatedDocumentCountOptions 객체에서 다음 속성을 설정할 수 있습니다.

속성
설명
MaxTime
The maximum amount of time that the query can run on the server.
Default: nil

다음 예는 students 컬렉션의 문서 수를 추정하는 예입니다:

var count = _myColl.EstimatedDocumentCount();
Console.WriteLine("Estimated number of documents in the students collection: " + count);

Count() 빌더 메서드를 사용하여 애그리게이션 파이프라인의 문서 수를 계산할 수 있습니다.

다음 예제에서는 다음 작업을 수행합니다:

  • FinalGrade 값이 80보다 큰 문서를 찾기 위한 일치 단계 지정

  • 기준과 일치하는 문서의 수 계산

var filter = Builders<Student>
.Filter.Gt(s => s.FinalGrade, 80);
var result = _myColl.Aggregate().Match(filter).Count();
Console.WriteLine("Number of documents with a final grade more than 80: " + result.First().Count);

언급된 작업에 대해 자세히 알아보려면 다음 가이드를 참조하세요.

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

돌아가기

데이터 조회

다음

데이터 변경 사항 모니터링