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

$indexOfCP(式演算子)

$indexOfCP

string8 から部分文字列を検索し、最初に出現した string の UTF- コード点のインデックス(ゼロベース)を返します。部分文字列が見つからない場合は、-1 が返されます。

$indexOfCP has the following operator expression syntax:

{ $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] }
フィールド
タイプ
説明

<string>

string

に変換される限り、任意の有効な stringを指定できます。式の詳細については、「式 」を参照してください。

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

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

<substring>

string

に変換される限り、任意の有効な stringを指定できます。式の詳細については、「式 」を参照してください。

<start>

integer

任意。検索の開始インデックス位置を指定する整数、または整数で表せる数値(2.0など)。負でない整数に変換される任意の有効なを指定できます。

指定しない場合、検索の開始インデックス位置は文字列の先頭になります。

<end>

integer

任意。検索の終了インデックス位置を指定する整数、または整数で表せる数値(2.0 など)。負でない整数に変換される任意の有効な式を指定できます。 <end><start>インデックス値を指定する場合は、 インデックス値も指定する必要があります。それ以外の場合、$indexOfCP<end> 値ではなく<start> インデックス値として<end> 値ではなく 値を使用します。

指定しない場合、検索の終了インデックス位置は文字列の末尾になります。

If the <substring expression> is found multiple times within the <string expression>, then $indexOfCP returns the index of the first <substring expression> found from the starting index position.

$indexOfCP returns null:

  • <string expression>が null の場合、または

  • <string expression> が入力ドキュメント内の存在しないフィールドを参照している場合。

$indexOfCP は以下の場合エラーを返します。

  • <string expression>が string ではなく null でない場合、または

  • <substring expression>が null または string ではなく、入力ドキュメントに存在しないフィールドを参照する場合、または

  • <start> または <end> が負の整数(または -5.0 のように負の整数として表せる値)の場合。

$indexOfCP returns -1:

  • 部分文字列が<string expression>で見つからない場合、または

  • <start><end>より大きい数値の場合、または

  • <start>が string のバイト長よりも大きい数値の場合。

結果

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

3

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

3

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

-1

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

4

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

7

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

-1

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

エラー

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

-1

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

-1

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

-1

{ $indexOfCP: [ 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 $indexOfCP operator to return the code point index at which the string foo is located in each item string:

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

この操作は次の結果を返します。

{ _id: 1, cpLocation: "0" }
{ _id: 2, cpLocation: "3" }
{ _id: 3, cpLocation: "4" }
{ _id: 4, cpLocation: "-1" }
{ _id: 5, cpLocation: null }
{ _id: 6, cpLocation: null }
このページを評価

項目一覧