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

$indexOfBytes (표현식 연산자)

$indexOfBytes

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

$indexOfBytes has the following operator expression syntax:

{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] }
피연산자
설명

<string expression>

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

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

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

<substring expression>

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

<start>

선택 사항 Atlas Search의 시작 인덱스 위치를 지정하는 정수입니다. 음수가 아닌 정수로 해석되는 모든 유효한 표현식 일 수 있습니다.

<end>

Optional An integral number 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, $indexOfBytes uses the <end> value as the <start> index value instead of the <end> value.

  • If <string expression> is null, $indexOfBytes returns null.

  • If $indexOfBytes is called on a field that doesn't exist in the document, $indexOfBytes returns null.

  • If <string expression> is not a string and not null, $indexOfBytes returns an error.

  • If <substring expression> is null, $indexOfBytes returns an error.

  • If <start> or <end> is a negative number, $indexOfBytes returns an error.

  • If <start> is a number greater than <end>, $indexOfBytes returns -1.

  • If <start> is a number greater than the byte length of the string, $indexOfBytes returns -1.

  • If <start> or <end> is given a value that is not an integer, $indexOfBytes returns an error.

  • If the <substring expression> is found multiple times within the <string expression>, then $indexOfBytes returns the index of the first <substring expression> found.

다양한 동작을 강조하기 위한 몇 가지 짧은 예:

예시
결과

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

3

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

3

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

-1

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

5

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

7

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

-1

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

-1

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

-1

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

-1

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

-1

{ $indexOfBytes: [ 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 $indexOfBytes operator to retrieve the indexes at which the string foo is located in each item:

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

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

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

이 페이지의 내용