Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
PHP 라이브러리 매뉴얼
/ /

고유 필드 값 검색

이 가이드 에서는 MongoDB PHP 라이브러리를 사용하여 컬렉션 전체에서 지정된 필드 의 고유 값을 조회 하는 방법을 학습 수 있습니다.

컬렉션 내에서 서로 다른 문서에 단일 필드 에 대해 서로 다른 값이 포함될 수 있습니다. 예를 예시 restaurants 컬렉션 의 한 문서 는 borough 값이 'Manhattan' 이고 다른 문서의 borough 값은 'Queens' 입니다. MongoDB PHP 라이브러리를 사용하면 컬렉션 의 여러 문서에서 필드 에 포함된 모든 고유 값을 조회 할 수 있습니다.

이 가이드 의 예제에서는 Atlas 샘플 데이터 세트sample_restaurants 데이터베이스 에 있는 restaurants 컬렉션 을 사용합니다. PHP 애플리케이션 에서 이 컬렉션 에 액세스 하려면 Atlas cluster 에 연결하는 MongoDB\Client 를 인스턴스화하고 $collection 변수에 다음 값을 할당합니다.

$collection = $client->sample_restaurants->restaurants;

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

지정된 필드의 고유 값을 검색하려면 MongoDB\Collection::distinct() 메서드를 호출하고 고유 값을 찾으려는 필드의 이름을 전달합니다.

다음 예에서는 restaurants 컬렉션에 있는 borough 필드의 고유 값을 검색합니다.

$results = $collection->distinct('borough', []);
foreach ($results as $value) {
echo json_encode($value), PHP_EOL;
}
"Bronx"
"Manhattan"
"Missing"
"Queens"
"Staten Island"

이 작업은 각 고유 borough 필드 값을 저장하는 배열 을 반환합니다. 여러 문서의 borough 필드 에 동일한 값이 있더라도 각 값은 결과에 한 번만 표시됩니다.

distinct() 메서드에 쿼리 필터하다 를 제공하여 컬렉션 의 문서 하위 집합에서 고유 필드 값을 찾을 수 있습니다. 쿼리 필터하다 는 작업에서 문서를 일치시키는 데 사용되는 검색 기준을 지정하는 표현식 입니다. 쿼리 필터하다 만들기에 대한 자세한 내용은 쿼리 지정 가이드 를 참조하세요.

다음 예에서는 cuisine 필드 값이 'Italian' 인 모든 문서에 대해 borough 필드의 고유 값을 검색합니다.

$results = $collection->distinct('borough', ['cuisine' => 'Italian']);
foreach ($results as $value) {
echo json_encode($value), PHP_EOL;
}
"Bronx"
"Manhattan"
"Queens"
"Staten Island"

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

옵션
설명

collation

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

maxTimeMS

The maximum amount of time in milliseconds that the operation can run.
Type: integer

comment

The comment to attach to the operation.
Type: any valid BSON type

readPreference

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

hint

The index to use for the operation.
Type: string|object

다음 예시 에서는 borough 필드 값이 'Bronx' 이고 cuisine 필드 값이 'Pizza' 인 모든 문서에 대해 name 필드 의 고유 값을 검색합니다. 또한 옵션 배열 의 comment 필드 를 지정하여 작업에 주석을 추가합니다.

$query = ['borough' => 'Bronx', 'cuisine' => 'Pizza'];
$options = ['comment' => 'Bronx pizza restaurants'];
$results = $collection->distinct('name', $query, $options);
foreach ($results as $value) {
echo json_encode($value), PHP_EOL;
}
"$1.25 Pizza"
"18 East Gunhill Pizza"
"2 Bros"
"Aenos Pizza"
"Alitalia Pizza Restaurant"
"Amici Pizza And Pasta"
"Angie'S Cafe Pizza"
...

작업에 대한 데이터 정렬을 지정하려면 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

(Optional) Specifies whether to include case comparison.

When set to true, the comparison behavior depends on the value of the strength field:

- If strength is 1, the PHP library compares base
characters and case.

- If strength is 2, the PHP library compares base
characters, diacritics, other secondary differences, and case.

- If strength is any other value, this field is ignored.

When set to false, the PHP library doesn't include case comparison at strength level 1 or 2.

Data Type: bool
Default: false

caseFirst

(Optional) Specifies the sort order of case differences during tertiary level comparisons.

Data Type: string
Default: "off"

strength


Data Type: int
Default: 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

(Optional) Specifies whether the library considers whitespace and punctuation as base characters for comparison purposes.

Data Type: string
Default: "non-ignorable"

maxVariable

(Optional) Specifies which characters the library considers ignorable when the alternate field is set to "shifted".

Data Type: string
Default: "punct"

backwards

(Optional) Specifies whether strings containing diacritics sort from the back of the string to the front.

Data Type: bool
Default: false

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

distinct() 메서드에 학습 보려면 API 설명서에서 MongoDB\Collection::distinct() 를 참조하세요.

돌아가기

문서 수 계산

이 페이지의 내용