On this page本页内容
$arrayToObject¶New in version 3.4.4.
Converts an array into a single document; the array must be either:将数组转换为单个文档;该数组必须是:
- OR -
k and v where:k和v的文档数组,其中:
k field contains the field name.k字段包含字段名。v field contains the value of the field.v字段包含该字段的值。$arrayToObject has the following syntax:语法如下所示:
The <expression> can be any valid expression that resolves to an array of two-element arrays or array of documents that contains “k” and “v” fields.<expression>可以是解析为两个元素数组的数组或包含“k”和“v”字段的文档数组的任何有效表达式。
For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式。
If the name of a field repeats in the array,如果字段的名称在数组中重复,
$arrayToObject uses the last value for that field. $arrayToObject使用该字段的最后一个值。$arrayToObject uses the last value for that field. $arrayToObject使用该字段的最后一个值。$arrayToObject uses the last value for that field. $arrayToObject使用该字段的最后一个值。|
|
$arrayToObjectConsider a 考虑一个inventory collection with the following documents:inventory集合带有以下文档:
The following aggregation pipeline operation use the 以下聚合管道操作使用$arrayToObject to return the dimensions field as a document:$arrayToObject将dimensions字段作为文档返回:
The operation returns the following:该操作返回以下内容:
Starting in versions 4.0.5+ (3.6.10+ and 3.4.19+), if the name of a field repeats in the array, 从版本4.0.5+(3.6.10+和3.4.19+)开始,如果某个字段的名称在数组中重复,$arrayToObject uses the last value for that field.$arrayToObject将使用该字段的最后一个值。
$objectToArray + $arrayToObject Example¶Consider a 考虑一个inventory collection with the following documents:inventory集合带有以下文档:
The following aggregation pipeline operation calculates the total in stock for each item and adds to the 以下聚合管道操作计算每个项目的总库存,并将其添加到instock document:instock文档中:
The operation returns the following:该操作返回以下内容:
See also参阅