AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

문서 수 계산

이 가이드 에서는 MongoDB PHP 라이브러리를 사용하여 컬렉션 에 있는 문서 수의 정확한 예상 개수를 조회 하는 방법을 학습 수 있습니다. 다음 메서드는 컬렉션 의 문서 수를 계산합니다.

  • MongoDB\Collection::countDocuments(): 쿼리 필터하다 와 일치하거나 컬렉션 에 존재하는 문서의 정확한 수를 반환합니다.

  • MongoDB\Collection::estimatedDocumentCount(): 컬렉션 의 예상 문서 수를 반환합니다.

The examples in this guide use the companies collection in the sample_training database from the Atlas sample datasets. To access this collection from your PHP application, instantiate a MongoDB\Client that connects to an Atlas cluster and assign the following value to your $collection variable:

$collection = $client->sample_training->companies;

무료 MongoDB 배포서버 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면, MongoDB 시작하기 가이드 참조하세요.

컬렉션 의 문서 수를 계산하려면 MongoDB\Collection::countDocuments() 메서드를 사용합니다. 특정 검색 기준과 일치하는 문서 수를 계산하려면 countDocuments() 메서드에 쿼리 필터하다 를 전달합니다.

쿼리 지정에 대해 자세히 알아보려면 쿼리 지정 가이드를 참조하세요.

컬렉션 의 모든 문서 수를 반환하려면 다음 예시 와 같이 빈 쿼리 필터하다 배열 을 countDocuments() 메서드에 전달합니다.

$result = $collection->countDocuments([]);
echo 'Number of documents: ', $result;

특정 검색 기준과 일치하는 문서 수를 반환하려면 countDocuments() 메서드에 쿼리 필터하다 를 전달합니다.

다음 예시 에서는 founded_year 필드 의 값이 2010 인 문서 수를 계산합니다.

$result = $collection->countDocuments(['founded_year' => 2010]);
echo 'Number of companies founded in 2010: ', $result;

옵션 값을 지정하는 배열 을 전달하여 countDocuments() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 카운트 작업을 사용자 지정하기 위해 설정하다 수 있는 몇 가지 옵션을 설명합니다.

옵션
설명

collation

The collation to use for the operation. To learn more, see the Collation section of this page.
Type: array|object

hint

작업에 사용할 인덱스입니다.
유형: string|array|object

comment

작업에 참부할 주석입니다.
유형: 유효한 BSON 유형

limit

반환할 문서의 최대 개수. 이 값은 양의 정수여야 합니다.
유형: integer

maxTimeMS

작업이 실행될 수 있는 최대 시간(밀리초)입니다.
유형: integer

skip

문서를 계산하기 전에 건너랄을 문서 수입니다.
유형: integer

readPreference

The read preference to use for the operation. To learn more, see Read Preference in the Server manual.
Type: MongoDB\Driver\ReadPreference

다음 예시 에서는 countDocuments() 메서드를 사용하여 number_of_employees 필드 의 값이 50 인 문서 수를 계산하고 최대 100 결과를 계산하도록 작업에 지시합니다.

$result = $collection->countDocuments(
['number_of_employees' => 50],
['limit' => 100],
);
echo 'Number of companies with 50 employees: ', $result;

작업에 대한 데이터 정렬을 지정하려면 collation 옵션을 설정하는 $options 배열 매개변수를 작업 메서드에 전달합니다. 데이터 정렬 규칙을 구성하는 배열 에 collation 옵션을 할당합니다.

다음 표에서는 데이터 정렬을 구성하기 위해 설정하다 수 있는 필드에 대해 설명합니다.

필드
설명

locale

(Required) Specifies the International Components for Unicode (ICU) locale. For a list of supported locales, see Collation Locales and Default Parameters in the MongoDB Server manual.

Data Type: string

caseLevel

(선택 사항) 대/소문자 비교 포함 여부를 지정합니다.

으로 true설정하면 비교 동작은 strength

strength 1필드의 값에 따라
달라집니다. - 가 인 경우 PHP 라이브러리는 기본 문자와 대/소문자를 비교합니다.

- 가 strength 2인 경우 PHP 라이브러리는 기본
문자, 양음 부호, 기타 세컨더리 차이 및 대/소문자를 비교합니다.

- strength 이 다른 값인 경우 이 필드는 무시됩니다.

false으로 설정하면 PHP 라이브러리는 강도 수준 1 또는 2에서 대/소문자 비교를 포함하지 않습니다.

데이터 유형: bool
기본값: false

caseFirst

(선택 사항) 3차 수준 비교 시 대소문자 차이의 정렬 순서를 지정합니다.

데이터 유형: string
기본값: "off"

strength

(선택 사항) ICU 문서에 정의된 대로 수행할 비교 수준을 지정합니다.

데이터 유형: int
기본값: 3

numericOrdering

(Optional) Specifies whether the driver compares numeric strings as numbers.

If set to true, the PHP library compares numeric strings as numbers. For example, when comparing the strings "10" and "2", the library uses the strings' numeric values and treats "10" as greater than "2".

If set to false, the PHP library compares numeric strings as strings. For example, when comparing the strings "10" and "2", the library compares one character at a time and treats "10" as less than "2".

For more information, see Collation Restrictions in the MongoDB Server manual.

Data Type: bool
Default: false

alternate

(선택 사항) 비교를 위해 라이브러리에서 공백과 구듁점을 기본 문자로 간주할지 여부를 지정합니다.

데이터 유형: string
기본값: "non-ignorable"

maxVariable

(선택 사항) alternate 필드가 "shifted"(으)로 설정되어 있을 때 라이브러리가 무시할 수 있는 문자를 지정합니다.

데이터 유형: string
기본값: "punct"

backwards

(선택 사항) 분음 부호가 포함된 string이 string의 뒤쪽에서 앞쪽으로 정렬되는지 지정합니다.

데이터 유형: bool
기본값: false

데이터 정렬 및 각 필드에 사용할 수 있는 값에 대해 자세히 학습하려면 MongoDB Server 매뉴얼의 데이터 정렬 항목을 참조하세요.

MongoDB\Collection::estimatedDocumentCount() 메서드를 호출하여 컬렉션 에 있는 문서 수의 추정치를 조회 할 수 있습니다. 이 메서드는 컬렉션 메타데이터 를 기반으로 문서의 양을 추정하며, 이는 정확한 개수를 계산하는 것보다 빠를 수 있습니다.

다음 예에서는 컬렉션의 문서 수를 추정합니다.

$result = $collection->estimatedDocumentCount();
echo 'Estimated number of documents: ', $result;

옵션 값을 매개 변수로 지정하는 배열 을 전달하여 estimatedDocumentCount() 메서드의 동작을 수정할 수 있습니다. 다음 표에서는 배열 에서 설정하다 수 있는 옵션에 대해 설명합니다.

옵션
설명

comment

작업에 참부할 주석입니다.
유형: 유효한 BSON 유형

maxTimeMS

작업이 실행될 수 있는 최대 시간(밀리초)입니다.
유형: integer

readConcern

The read concern to use for the operation. To learn more, see Read Concern in the Server manual.
Type: MongoDB\Driver\ReadConcern

readPreference

The read preference to use for the operation. To learn more, see Read Preference in the Server manual.
Type: MongoDB\Driver\ReadPreference

session

작업과 연결할 클라이언트 세션입니다.
유형: MongoDB\Driver\Session

다음 예시 에서는 estimatedDocumentCount() 메서드를 사용하여 컬렉션 에 있는 문서 수의 추정치를 반환하고 작업에 1000 밀리초의 시간 제한을 설정합니다.

$result = $collection->estimatedDocumentCount(['maxTimeMS' => 1000]);
echo 'Estimated number of documents: ', $result;

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