정의
$substrCP문자열의 부분 문자열을 반환합니다. 하위 문자열은 지정된 코드 포인트 수만큼 문자열에서 지정된 UTF-8 코드 점 (CP) 인덱스 (0 기반)에 있는 문자로 시작합니다.
$substrCPhas the following operator expression syntax:{ $substrCP: [ <string expression>, <code point index>, <code point count> ] } 필드유형설명string expression문자열
부분 문자열이 추출될 문자열입니다.
string expression는 문자열로 해석되는 한 모든 유효한 표현식 일 수 있습니다. 표현식에 대한 자세한 내용은 표현식을 참조하세요 .If the argument resolves to a value of
nullor refers to a field that is missing,$substrCPreturns an empty string.If the argument does not resolve to a string or
nullnor refers to a missing field,$substrCPreturns an error.code point index숫자
하위 문자열의 시작점을 나타냅니다.
code point index는 음수가 아닌 정수로 해석되는 한 모든 유효한 표현식이 될 수 있습니다.code point count숫자
음수가 아닌 정수 또는 정수(예: 2.0)로 표현할 수 있는 숫자로 해석되는 한 모든 유효한 표현식 이 될 수 있습니다.
예시결과{ $substrCP: [ "abcde", 1, 2 ] }"bc"{ $substrCP: [ "Hello World!", 6, 5 ] }"World"{ $substrCP: [ "cafétéria", 0, 5 ] }"cafét"{ $substrCP: [ "cafétéria", 5, 4 ] }"éria"{ $substrCP: [ "cafétéria", 7, 3 ] }"ia"{ $substrCP: [ "cafétéria", 3, 1 ] }"é"
행동
The $substrCP operator uses the code points to extract the substring. This behavior differs from the $substrBytes operator which extracts the substring by the number of bytes, where each character uses between one and four bytes.
예시
싱글 바이트 문자 세트
다음 문서가 포함된 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 $substrCP operator to separate the quarter value 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 $strLenCP.
db.inventory.aggregate( [ { $project: { item: 1, yearSubstring: { $substrCP: [ "$quarter", 0, 2 ] }, quarterSubstring: { $substrCP: [ "$quarter", 2, { $subtract: [ { $strLenCP: "$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" } ] )
다음 예에서는 $substrCP 연산자를 사용하여 name 값에서 3바이트 menuCode를 생성합니다.
db.food.aggregate( [ { $project: { "name": 1, "menuCode": { $substrCP: [ "$name", 0, 3 ] } } } ] )
이 연산은 다음과 같은 결과를 반환합니다.
{ _id: 1, name: "apple", menuCode: "app" } { _id: 2, name: "banana", menuCode: "ban" } { _id: 3, name: "éclair", menuCode: "écl" } { _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: "寿司s" }