정의
$indexOfBytes문자열에서 하위 string 의 발생을 검색하고 첫 번째 항목의 UTF-8 바이트 인덱스 (0 기반)를 반환합니다. 부분 문자열을 찾을 수 없으면
-1을 반환합니다.$indexOfByteshas the following operator expression syntax:{ $indexOfBytes: [ <string expression>, <substring expression>, <start>, <end> ] } 피연산자설명<string expression>문자열로 해석되는 한 모든 유효한 표현식 일 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .
If the string expression resolves to a value of
nullor refers to a field that is missing,$indexOfBytesreturnsnull.If the string expression does not resolve to a string or
nullnor refers to a missing field,$indexOfBytesreturns an error.<substring expression><start>선택 사항 Atlas Search의 시작 인덱스 위치를 지정하는 정수입니다. 음수가 아닌 정수로 해석되는 모든 유효한 표현식 일 수 있습니다.
<end>선택 사항 검색 의 끝 인덱스 위치를 지정하는 정수입니다.
<end>음수가 아닌 정수로 해석되는 모든 유효한 표현식 일 수 있습니다.<start>인덱스 값을 지정하는 경우 인덱스 값도 지정해야$indexOfBytes합니다.<end><start><end>그렇지 않으면 은(는) 값 대신 값을 인덱스 값으로 사용합니다.
행동
If
<string expression>is null,$indexOfBytesreturnsnull.If
$indexOfBytesis called on a field that doesn't exist in the document,$indexOfBytesreturnsnull.이(가) 문자열이 아니고 null이 아닌
$indexOfBytes경우<string expression>은(는) 오류를 반환합니다.<substring expression>이 null이면 는 오류를 반환합니다.$indexOfBytes<start>또는<end>이 음수인 경우 은 오류를$indexOfBytes반환합니다.가
<start>보다 큰 숫자인<end>경우 은$indexOfBytes를-1반환합니다.<start>이 문자열의 바이트 길이보다 큰 숫자인$indexOfBytes경우-1은 을 반환합니다.또는 에
<start><end>정수가 아닌 값이 지정되면 는 오류를$indexOfBytes반환합니다.If the
<substring expression>is found multiple times within the<string expression>, then$indexOfBytesreturns the index of the first<substring expression>found.
다양한 동작을 강조하기 위한 몇 가지 짧은 예:
예시 | 결과 |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
예시
다음 문서가 포함된 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 } ] )
다음 작업은 $indexOfBytes 연산자 사용하여 각 foo 항목에서 문자열 이(가) 있는 인덱스를 조회 합니다.
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 }