$indexOfCP (aggregation)

Definition

  • $indexOfCP

New in version 3.4.

Searches a string for an occurence of a substring and returns theUTF-8 code pointindex (zero-based) of the first occurence. If thesubstring is not found, returns -1.

$indexOfCP has the following operatorexpression syntax:

  1. { $indexOfCP: [ <string expression>, <substring expression>, <start>, <end> ] }

FieldTypeDescription<string>stringCan be any valid expression as longas it resolves to a string. For more information on expressions, seeExpressions.

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

If the string expression does not resolve to a string or nullnor refers to a missing field, $indexOfCP returns anerror.<substring>stringCan be any valid expression as longas it resolves to a string. For more information on expressions, seeExpressions.<start>integerOptional. An integer, or a number that can be represented as integers (such as2.0), that specifies the starting index position for the search. Canbe any valid expression thatresolves to a non-negative integral number.

If unspecified, the starting index position for the search is thebeginning of the string.<end>integerOptional. An integer, or a number that can be represented as integers (such as2.0), that specifies the ending index position for the search. Canbe any valid expression thatresolves to a non-negative integral number. If you specify a<end> index value, you should also specify a <start> indexvalue; otherwise, $indexOfCP uses the <end> valueas the <start> index value instead of the <end> value.

If unspecified, the ending index position for the search is theend of the string.

Behavior

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

$indexOfCP returns null:

  • If <string expression> is null, or
  • If <string expression> refers to a non-existing field in theinput document.

$indexOfCP returns an error:

  • If <string expression> is not a string and not null, or
  • If <substring expression> is null or is not a string or refers toa nonexistent field in the input document, or
  • If <start> or <end> is a negative integer (or a value thatcan be represented as a negative integer, like -5.0).

$indexOfCP returns -1:

  • If the substring is not found in the <string expression>, or
  • If <start> is a number greater than <end>, or
  • If <start> is a number greater than the byte length of the string.
ExampleResults
{ $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 ] }Error
{ $indexOfCP: [ "vanilla", "ll", 12 ] }-1
{ $indexOfCP: [ "vanilla", "ll", 5, 2 ] }-1
{ $indexOfCP: [ "vanilla", "nilla", 3 ] }-1
{ $indexOfCP: [ null, "foo" ] }null

Examples

Consider an inventory collection with the following documents:

  1. { "_id" : 1, "item" : "foo" }
  2. { "_id" : 2, "item" : "fóofoo" }
  3. { "_id" : 3, "item" : "the foo bar" }
  4. { "_id" : 4, "item" : "hello world fóo" }
  5. { "_id" : 5, "item" : null }
  6. { "_id" : 6, "amount" : 3 }

The following operation uses the $indexOfCP operator toreturn the code point index at which the string foo is located ineach item string:

  1. db.inventory.aggregate(
  2. [
  3. {
  4. $project:
  5. {
  6. cpLocation: { $indexOfCP: [ "$item", "foo" ] },
  7. }
  8. }
  9. ]
  10. )

The operation returns the following results:

  1. { "_id" : 1, "cpLocation" : "0" }
  2. { "_id" : 2, "cpLocation" : "3" }
  3. { "_id" : 3, "cpLocation" : "4" }
  4. { "_id" : 4, "cpLocation" : "-1" }
  5. { "_id" : 5, "cpLocation" : null }
  6. { "_id" : 6, "cpLocation" : null }

See also

$indexOfBytes and $indexOfArray