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

$substrBytes (표현식 연산자)

$substrBytes

문자열의 부분 문자열을 반환합니다. 하위 문자열은 문자열에서 지정된 UTF-8 바이트 인덱스(0부터 시작)에 있는 문자로 시작하여 지정된 바이트 수만큼 계속됩니다.

$substrBytes has the following operator expression syntax:

{ $substrBytes: [ <string expression>, <byte index>, <byte count> ] }
필드
유형
설명

string expression

문자열

부분 문자열이 추출될 문자열입니다. string expression 는 문자열로 해석되는 한 모든 유효한 표현식 일 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .

If the argument resolves to a value of null or refers to a field that is missing, $substrBytes returns an empty string.

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

byte index

숫자

부분 문자열의 시작점을 나타냅니다. byte index 은(는) 음수가 아닌 정수 또는 정수(예: 2.0)로 표현할 수 있는 숫자로 해석되는 한 모든 유효한 표현식 이 될 수 있습니다.

byte index 멀티바이트 UTF-8 문자 중간에 있는 시작 인덱스를 참조할 수 없습니다.

byte count

숫자

음수가 아닌 정수 또는 정수(예: 2.0)로 표현할 수 있는 숫자로 해석되는 한 모든 유효한 표현식 이 될 수 있습니다.

byte count 는 UTF-8 문자 중간에 있는 끝 인덱스를 생성할 수 없습니다.

The $substrBytes operator uses the indexes of UTF-8 encoded bytes where each code point, or character, may use between one and four bytes to encode.

예를 예시, US-ASCII 문자는 1바이트를 사용하여 인코딩됩니다. 분음 부호와 추가 라틴 알파벳 문자(영어 알파벳 이외의 라틴 문자)가 있는 문자는 2바이트를 사용하여 인코딩됩니다. 중국어, 일본어 및 한국어 문자에는 일반적으로 3바이트가 필요하고, 다른 유니코드 평면(이모티콘, 수학 기호 등)에는 4바이트가 필요합니다.

UTF-8 문자 중간에 byte index 또는 byte count 를 제공하면 오류가 발생하므로 string expression 의 내용에 유의하는 것이 중요합니다.

$substrBytes differs from $substrCP in that $substrBytes counts the bytes of each character, whereas $substrCP counts the code points, or characters, regardless of how many bytes a character uses.

예시
결과
{ $substrBytes: [ "abcde", 1, 2 ] }
"bc"
{ $substrBytes: [ "Hello World!", 6, 5 ] }
"World"
{ $substrBytes: [ "cafétéria", 0, 5 ] }
"café"
{ $substrBytes: [ "cafétéria", 5, 4 ] }
"tér"
{ $substrBytes: [ "cafétéria", 7, 3 ] }

메시지 오류:

"Error: Invalid range, starting index is a UTF-8 continuation byte."

{ $substrBytes: [ "cafétéria", 3, 1 ] }

메시지 오류:

"Error: Invalid range, ending index is in the middle of a UTF-8 character."

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

db.inventory.insertMany( [
{ _id: 1, item: "ABC1", quarter: "13Q1", description: "product 1" },
{ _id: 2, item: "ABC2", quarter: "13Q4", description: "product 2" },
{ _id: 3, item: "XYZ1", quarter: "14Q2", description: null }
] )

The following operation uses the $substrBytes operator separate the quarter value (containing only single byte US-ASCII characters) into a yearSubstring and a quarterSubstring. The quarterSubstring field represents the rest of the string from the specified byte index following the yearSubstring. It is calculated by subtracting the byte index from the length of the string using $strLenBytes.

db.inventory.aggregate(
[
{
$project: {
item: 1,
yearSubstring: { $substrBytes: [ "$quarter", 0, 2 ] },
quarterSubstring: {
$substrBytes: [
"$quarter", 2, { $subtract: [ { $strLenBytes: "$quarter" }, 2 ] }
]
}
}
}
]
)

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

{ _id: 1, item: "ABC1", yearSubstring: "13", quarterSubstring: "Q1" }
{ _id: 2, item: "ABC2", yearSubstring: "13", quarterSubstring: "Q4" }
{ _id: 3, item: "XYZ1", yearSubstring: "14", quarterSubstring: "Q2" }

다음 문서로 food 컬렉션을 생성합니다.

db.food.insertMany(
[
{ _id: 1, name: "apple" },
{ _id: 2, name: "banana" },
{ _id: 3, name: "éclair" },
{ _id: 4, name: "hamburger" },
{ _id: 5, name: "jalapeño" },
{ _id: 6, name: "pizza" },
{ _id: 7, name: "tacos" },
{ _id: 8, name: "寿司sushi" }
]
)

다음 작업은 $substrBytes 연산자를 사용하여 name 값에서 3바이트 menuCode 를 생성합니다.

db.food.aggregate(
[
{
$project: {
"name": 1,
"menuCode": { $substrBytes: [ "$name", 0, 3 ] }
}
}
]
)

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

{ _id: 1, name: "apple", menuCode: "app" }
{ _id: 2, name: "banana", menuCode: "ban" }
{ _id: 3, name: "éclair", menuCode: "éc" }
{ _id: 4, name: "hamburger", menuCode: "ham" }
{ _id: 5, name: "jalapeño", menuCode: "jal" }
{ _id: 6, name: "pizza", menuCode: "piz" }
{ _id: 7, name: "tacos", menuCode: "tac" }
{ _id: 8, name: "寿司sushi", menuCode: "寿" }
이 페이지 평가하기

이 페이지의 내용