$indexOfArray (aggregation)
On this page本页内容
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: [ <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
returnsnull
.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>
为null
,或者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>
是一个大于数组长度的数字。
{ $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:$indexOfArray
在items
数组中查找2
:
db.inventory.aggregate( [ {
$project: {
index: { $indexOfArray: [ "$items", 2 ] }
}
} ] )
The example returns:示例返回:
The first array index for the value每个项数组中值2
in eachitems
array, if found.2
的第一个数组索引(如果找到)。Array indexes start at数组索引从0
.0
开始。如果-1
for the index if2
is not in theitems
array.2
不在items
数组中,则索引为-1
。如果null
for the index ifitems
is not an array oritems
does not exist.items
不是数组或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 }
]