Database Manual / Reference / Query Language / Expressions

$indexOfArray (expression operator)(表达式运算符)

Definition定义

$indexOfArray

Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. Array indexes start at zero.在数组中搜索指定值的匹配项,并返回第一个匹配项的数组索引。数组索引从零开始。

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

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

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 express>的索引。

$indexOfArray returns null:返回null

  • If <array expression> is null, or如果<array expression>为空,或
  • If <array expression> refers to a non-existing field in the input document.如果<array expression>引用了输入文档中不存在的字段。

$indexOfArray returns an error:返回错误:

  • If <array expression> is not an array and not null, or如果<array expression>不是数组且不是null,或者
  • If <start> or <end> is a negative integer (or a value that can be represented as a negative integer, like -5.0).如果<start><end>是负整数(或可以表示为负整数的值,如-5.0)。

$indexOfArray returns -1:返回-1

  • If the <search expression> is not found in the array, or如果在数组中找不到<search expression>,或者
  • If <start> is a number greater than <end>, or如果<start>是一个大于<end>的数字,或者
  • If <start> is a number greater than the length of the array.如果<start>是一个大于数组长度的数字。
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

Example示例

The example uses this inventory collection:该示例使用此inventory集合:

db.inventory.insertMany( [
{ _id: 0, items: [ "one", "two", "three" ] },
{ _id: 1, items: [ 1, 2, 3 ] },
{ _id: 2, items: [ 1, 2, 3, 2 ] },
{ _id: 3, items: [ null, null, 2 ] },
{ _id: 4, items: [ 2, null, null, 2 ] },
{ _id: 5, items: null },
{ _id: 6, amount: 3 }
] )

The following example uses $indexOfArray to find 2 in the items array:以下示例使用$indexOfArrayitems数组中查找2

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

The example returns:示例返回:

  • The first array index for the value 2 in each items array, if found. Array indexes start at 0.每个items数组中值2的第一个数组索引(如果找到)。数组索引从0开始。
  • -1 for the index if 2 is not in the items array.如果2不在items数组中,则索引为-1
  • null for the index if items is not an array or items does not exist.如果items不是数组或项不存在,则索引为null

Example output:示例输出:

[
{ _id: 0, index: -1 },
{ _id: 1, index: 1 },
{ _id: 2, index: 1 },
{ _id: 3, index: 2 },
{ _id: 4, index: 0 },
{ _id: 5, index: null },
{ _id: 6, index: null }
]