AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

$substrCP(式演算子)

$substrCP

string の部分文字列を返します。部分文字列は、指定されたコード ポイント数の string 内の指定された UTF-8 コード点(CP)インデックス(ゼロ ベース)の文字から始まります。

$substrCP has the following operator expression syntax:

{ $substrCP: [ <string expression>, <code point index>, <code point count> ] }
フィールド
タイプ
説明

string expression

string

部分文字列が抽出される string。 string expressionは、string に変換される限り、任意の有効な式にすることができます。 式の詳細については、「式 」を参照してください。

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

If the argument does not resolve to a string or null nor refers to a missing field, $substrCP returns 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" }
このページを評価

項目一覧