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

MongoDB\Collection::createIndex()

MongoDB\Collection::createIndex()

collection에 대한 인덱스를 만듭니다.

function createIndex(
array|object $key,
array $options = []
): string
$key : 배열|객체

인덱싱할 필드와 인덱스 순서를 지정합니다.

예를 들어 다음은 username 필드에 내림차순 인덱스를 지정합니다.

[ 'username' => -1 ]
$options : 배열

원하는 옵션을 지정하는 배열입니다.

$options 매개변수는 인덱스 옵션 명령 옵션을 모두 허용합니다. 다음은 인덱스 옵션의 비완전한 목록입니다. 인덱스 옵션의 전체 목록은 MongoDB 매뉴얼의 createIndexes 명령 참조를 참조하세요.

인덱스 옵션 (비제한적)

데이터 정렬
배열|객체
Collation allows users to specify language-specific rules for string comparison, such as rules for lettercase and accent marks. When specifying collation, the locale field is mandatory; all other collation fields are optional. For descriptions of the fields, see Collation Document.If the collation is unspecified but the collection has a default collation, the operation uses the collation specified for the collection. If no collation is specified for the collection or for the operation, MongoDB uses the simple binary comparison used in prior versions for string comparisons.

expireAfterSeconds

integer

TTL 인덱스를 생성합니다.

이름

문자열

인덱스를 고유하게 식별하는 이름입니다. 기본적으로 MongoDB는 키를 기반으로 인덱스 이름을 생성합니다.

부분 필터 표현식

배열|객체

부분 인덱스 를 만듭니다.

희박

부울

희소 인덱스를 생성합니다.

unique

부울

고유 인덱스를 생성합니다.

명령 옵션

이름
유형
설명

comment

혼합

사용자가 데이터베이스 프로파일러, currentOp 출력 및 로그를 통해 작업을 추적하는 데 도움이 되는 임의의 주석을 지정할 수 있습니다.

이 옵션은 MongoDB 4.4부터 사용할 수 있으며 이전 서버 버전에 대해 지정된 경우 실행 시 예외가 발생합니다.

버전 1.13에 추가 되었습니다.

commitQuorum

문자열|정수

프라이머리가 인덱스를 준비됨으로 표시하기 전에 프라이머리를 포함한 복제본 세트의 데이터 보유 노드가 인덱스 빌드를 성공적으로 완료해야 하는 멤버의 수를 지정합니다.

이 옵션은 쓰기 고려 (write concern)의 w 필드에 더하기 "votingMembers" 에 대해 동일한 값을 허용하며, 이는 모든 투표 데이터 보유 노드를 나타냅니다.

이 기능은 4.4 이전의 서버 버전에서는 지원되지 않으며, 사용 시 실행 시 예외가 발생합니다.

버전 1.7에 추가 되었습니다.

최대 시간 (MS)

integer

커서에서 작업을 처리하는 데 걸리는 누적 시간 제한(밀리초)입니다. MongoDB는 가장 빠른 다음 MongoDB는 중단 지점 이후 가장 빠른 시점에 작업을 중단합니다.

버전 1.3에 추가 되었습니다.

Session

작업과 연결할 클라이언트 세션입니다.

버전 1.3에 추가 되었습니다.

쓰기 고려

작업에 사용할 쓰기 고려입니다. 기본값은 컬렉션의 쓰기 고려입니다.

트랜잭션의 일부로 개별 작업에 대한 쓰기 고려를 지정할 수 없습니다. 대신 트랜잭션을 시작할 때 writeConcern 옵션을 설정합니다.

생성된 인덱스의 이름(문자열)입니다.

옵션이 사용되지만 선택한 서버에서 지원되지 않는 경우 MongoDB\Exception\UnsupportedException입니다(예: collation, readConcern, writeConcern).

MongoDB\Exception\InvalidArgumentException 매개변수 또는 옵션의 구문 분석과 관련된 오류의 경우입니다.

확장 수준의 기타 오류에 대한MongoDB\ 드라이버\Exception\RuntimeException (예: 연결 오류).

The following example creates a compound index on the borough and cuisine fields in the restaurants collection in the test database.

<?php
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
$indexName = $collection->createIndex(['borough' => 1, 'cuisine' => 1]);
var_dump($indexName);

이 경우 출력은 다음과 유사합니다:

string(19) "borough_1_cuisine_1"

The following example adds a partial index on the borough field in the restaurants collection in the test database. The partial index indexes only documents where the borough field exists.

<?php
$collection = (new MongoDB\Client)->selectCollection('test', 'restaurants');
$indexName = $collection->createIndex(
['borough' => 1],
[
'partialFilterExpression' => [
'borough' => ['$exists' => true],
],
]
);
var_dump($indexName);

이 경우 출력은 다음과 유사합니다:

string(9) "borough_1"