$indexOfArray (aggregation)

On this page本页内容

Definition定义

$indexOfArray

New in version 3.4.版本3.4中的新功能。

Searches an array for an occurrence of a specified value and returns the array index (zero-based) of the first occurrence. 在数组中搜索指定值的匹配项,并返回第一个匹配项的数组索引(从零开始)。If the value is not found, returns -1.如果找不到该值,则返回-1

$indexOfArray has the following operator expression syntax:具有以下运算符表达式语法

{ $indexOfArray: [ <array expression>, <search expression>, <start>, <end> ] }
Field字段Type类型Description描述
<array> string

Can be any valid expression as long as it resolves to an array. 可以是任何有效的表达式,只要它解析为数组。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式

If the array expression resolves to a value of null or refers to a field that is missing, $indexOfArray returns null.如果数组表达式解析为null值或引用缺少的字段,$indexOfArray返回null

If the array expression does not resolve to an array or null nor refers to a missing field, $indexOfArray returns an error.如果数组表达式没有解析为数组或null,也没有引用缺少的字段,$indexOfArray返回一个错误。

<search value> string Can be any valid expression. 可以是任何有效的表达式For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式
<start> integer

Optional.可选。An integer, or a number that can be represented as integers (such as 2.0), that specifies the starting index position for the search. 一个整数,或可以表示为整数的数字(如2.0),用于指定搜索的起始索引位置。Can be any valid expression that resolves to a non-negative integral number.可以是解析为非负整数的任何有效表达式

If unspecified, the starting index position for the search is the beginning of the string.如果未指定,搜索的起始索引位置是字符串的开头。

<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. 一个整数,或可以表示为整数的数字(如2.0),用于指定搜索的结束索引位置。Can be any valid expression that resolves to a non-negative integral number. 可以是解析为非负整数的任何有效表达式If you specify a <end> index value, you should also specify a <start> index value; otherwise, $indexOfArray uses the <end> value as the <start> index value instead of the <end> value.如果指定<end>索引值,还应指定<start>索引值;否则,$indexOfArray<end>值用作<start>索引值,而不是<end>值。

If unspecified, the ending index position for the search is the end of the string.如果未指定,搜索的结束索引位置是字符串的结尾。

Behavior行为

If the <search expression> is found multiple times within the <array expression>, then $indexOfArray returns the index of the first <search expression> from the starting index position.如果在<array expression>中多次找到<search expression>,则$indexOfArray从起始索引位置返回第一个<search expression>的索引。

$indexOfArray returns null:返回null

$indexOfArray returns an error:返回一个错误:

$indexOfArray returns -1:返回-1:

Example示例Results结果
{ $indexOfArray: [ [ "a", "abc" ], "a" ] } 0
{ $indexOfArray: [ [ "a", "abc", "de", ["de"] ], ["de"] ] } 3
{ $indexOfArray: [ [ 1, 2 ], 5 ] } -1
{ $indexOfArray: [ [ 1, 2, 3 ], [1, 2] ] } -1
{ $indexOfArray: [ [ 10, 9, 9, 8, 9 ], 9, 3 ] } 4
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 0, 1 ] } -1
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 1, 0 ] } -1
{ $indexOfArray: [ [ "a", "abc", "b" ], "b", 20 ] } -1
{ $indexOfArray: [ [ null, null, null ], null ] } 0
{ $indexOfArray: [ null, "foo" ] } null
{ $indexOfArray: [ "foo", "foo" ] } Error

Examples示例

Consider an inventory collection with the following documents:考虑带有以下文档的inventory集合:

{ "_id" : 1, "items" : ["one", "two", "three"] }
{ "_id" : 2, "items" : [1, 2, 3] }
{ "_id" : 3, "items" : [null, null, 2] }
{ "_id" : 4, "items" : null }
{ "_id" : 5, "amount" : 3 }

The following operation uses the $indexOfArray operator to return the array index at which the string foo is located in each items array:以下操作使用$indexOfArray运算符返回字符串foo在每个items数组中所处的数组索引:

db.inventory.aggregate(
   [
     {
       $project:
          {
            index: { $indexOfArray: [ "$items", 2 ] },
          }
      }
   ]
)

The operation returns the following results:操作返回以下结果:

{ "_id" : 1, "index" : "-1" }
{ "_id" : 2, "index" : "1" }
{ "_id" : 3, "index" : "2" }
{ "_id" : 4, "index" : null }
{ "_id" : 5, "index" : null }

See also参阅

$indexOfBytes, $indexOfCP, and $in