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

$indexOfCP (표현식 연산자)

$indexOfCP

문자열에서 하위 문자열의 발생을 검색하고 첫 번째 항목의 UTF-8 코드 점 인덱스 (0 기반)를 반환합니다. 하위 문자열을 찾을 수 없으면 -1를 반환합니다.

$indexOfCP has the following operator expression syntax:

{ $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] }
필드
유형
설명

<string>

문자열

문자열로 해석되는 한 모든 유효한 표현식 일 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .

If the string expression resolves to a value of null or refers to a field that is missing, $indexOfCP returns null.

If the string expression does not resolve to a string or null nor refers to a missing field, $indexOfCP returns an error.

<substring>

문자열

문자열로 해석되는 한 모든 유효한 표현식 일 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .

<start>

integer

선택 사항. 정수 또는 검색의 시작 인덱스 위치를 지정하는 정수(예: 2.0)로 표시할 수 있는 숫자입니다. 음수가 아닌 정수로 해석되는 유효한 표현식이 될 수 있습니다.

지정하지 않으면 검색의 시작 인덱스 위치는 문자열의 시작입니다.

<end>

integer

Optional. An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid expression that resolves to a non-negative integral number. If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfCP uses the <end> value as the <start> index value instead of the <end> value.

지정하지 않으면 검색의 끝 인덱스 위치는 문자열의 끝입니다.

If the <substring expression> is found multiple times within the <string expression>, then $indexOfCP returns the index of the first <substring expression> found from the starting index position.

$indexOfCP returns null:

  • <string expression>이(가) null인 경우, 또는

  • <string expression>이 입력 문서에 존재하지 않는 필드를 참조하는 경우

$indexOfCP 은(는) 오류를 반환합니다.

  • <string expression> 가 문자열이 아니고 null이 아닌 경우 또는

  • <substring expression> 가 null이거나 문자열이 아니거나 입력 문서에 존재하지 않는 필드를 참조하는 경우 또는

  • <start> 또는 <end>가 음의 정수(또는 -5.0과 같이 음의 정수로 표시될 수 있는 값)인 경우

$indexOfCP returns -1:

  • <string expression> 에서 부분 문자열을 찾을 수 없는 경우 또는

  • <start>가 <end>보다 큰 숫자인 경우 또는

  • <start> 가 문자열의 바이트 길이보다 큰 숫자인 경우

예시
결과

{ $indexOfCP: [ "cafeteria", "e" ] }

3

{ $indexOfCP: [ "cafétéria", "é" ] }

3

{ $indexOfCP: [ "cafétéria", "e" ] }

-1

{ $indexOfCP: [ "cafétéria", "t" ] }

4

{ $indexOfCP: [ "foo.bar.fi", ".", 5 ] }

7

{ $indexOfCP: [ "vanilla", "ll", 0, 2 ] }

-1

{ $indexOfCP: [ "vanilla", "ll", -1 ] }

오류

{ $indexOfCP: [ "vanilla", "ll", 12 ] }

-1

{ $indexOfCP: [ "vanilla", "ll", 5, 2 ] }

-1

{ $indexOfCP: [ "vanilla", "nilla", 3 ] }

-1

{ $indexOfCP: [ null, "foo" ] }

null

다음 문서가 포함된 inventory 컬렉션을 생각해 보세요.

db.inventory.insertMany( [
{ _id: 1, item: "foo" }
{ _id: 2, item: "fóofoo" }
{ _id: 3, item: "the foo bar" }
{ _id: 4, item: "hello world fóo" }
{ _id: 5, item: null }
{ _id: 6, amount: 3 }
] )

The following operation uses the $indexOfCP operator to return the code point index at which the string foo is located in each item string:

db.inventory.aggregate(
[
{
$project:
{
cpLocation: { $indexOfCP: [ "$item", "foo" ] },
}
}
]
)

이 연산은 다음과 같은 결과를 반환합니다.

{ _id: 1, cpLocation: "0" }
{ _id: 2, cpLocation: "3" }
{ _id: 3, cpLocation: "4" }
{ _id: 4, cpLocation: "-1" }
{ _id: 5, cpLocation: null }
{ _id: 6, cpLocation: null }
이 페이지 평가하기

이 페이지의 내용