$indexOfCP (aggregation)
定義
$indexOfCP
Searches a string for an occurrence of a substring and returns the UTF-8 code point index (zero-based) of the first occurrence. If the substring is not found, returns
-1
.$indexOfCP
には次の演算子式の構文があります。{ $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] } フィールドタイプ説明<string>
string
Can be any valid 式 as long as it resolves to a string. For more information on expressions, see 式演算子.
If the string expression resolves to a value of
null
or refers to a field that is missing,$indexOfCP
returnsnull
.If the string expression does not resolve to a string or
null
nor refers to a missing field,$indexOfCP
returns an error.<substring>
string
<start>
integer
任意。検索の開始インデックス位置を指定する整数、または整数で表せる数値(2.0など)。負でない整数に変換される任意の有効な式を指定できます。
指定しない場合、検索の開始インデックス位置は文字列の先頭になります。
<end>
integer
Optional. An integer, or a number that can be represented as integers (such as 2.0), that specifies the ending index position for the search. Can be any valid 式 that resolves to a non-negative integral number. If you specify a
<end>
index value, you should also specify a<start>
index value; otherwise,$indexOfCP
uses the<end>
value as the<start>
index value instead of the<end>
value.指定しない場合、検索の終了インデックス位置は文字列の末尾になります。
動作
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
はnull
を返します。
<string expression>
が null の場合、またはIf
<string expression>
refers to a non-existing field in the input document.
$indexOfCP
は以下のエラーを返します。
If
<string expression>
is not a string and not null, orIf
<substring expression>
is null or is not a string or refers to a nonexistent field in the input document, or<start>
または<end>
が負の整数(または -5.0 のように負の整数として表せる値)の場合。
$indexOfCP
は-1
を返します。
If the substring is not found in the
<string expression>
, or<start>
が<end>
より大きい数値の場合、またはIf
<start>
is a number greater than the byte length of the string.
例 | 結果 |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
| エラー |
|
|
|
|
|
|
|
|
例
以下のドキュメントを持つinventory
コレクションを検討してください。
{ "_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 }
以下も参照してください。