In MongoDB, the $arrayToObject
aggregation pipeline operator converts an array to a document.
The array provided to $arrayToObject
must be in one of the two following formats:
- An array of two-element arrays where the first element is the field name, and the second element is the field value.
- An array of documents that contain a
k
field and av
field, where thek
field contains the field name and thev
field contains the value.
Format 1
Suppose we have a collection called test
with the following document:
{ "_id" : 1, "data" : [ [ "name", "Fetch" ], [ "type", "Dog" ] ] }
We can use the $arrayToObject
operator to return the data
field as a document object:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 1 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
{ "result" : { "name" : "Fetch", "type" : "Dog" } }
Format 2
Suppose we have a document like this:
{ "_id" : 2, "data" : [ { "k" : "name", "v" : "Fetch" }, { "k" : "type", "v" : "Dog" } ] }
In this case, the k
fields contain the keys and the v
fields contain the values.
Here’s what happens when we apply $arrayToObject
to that document:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 2 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
{ "result" : { "name" : "Fetch", "type" : "Dog" } }
We can see that this resulted in the same document that was produced in the previous example.
Non-Conforming Arrays
The argument provided to $arrayToObject
can be any valid expression as long as it resolves to an array of two-element arrays or array of documents that contains k
and v
fields.
If the argument does not adhere to this, an error occurs.
Suppose we have the following document:
{ "_id" : 3, "data" : [ [ "name", "Fetch", "Dog" ] ] }
This array contains three elements.
Here’s what happens when we apply $arrayToObject
to that document:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 3 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$arrayToObject requires an array of size 2 arrays,found array of size: 3", "code" : 40397, "codeName" : "Location40397" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1
As the error states, $arrayToObject requires an array of size 2 arrays
.
Here’s another document that contains a non-conforming array:
{ "_id" : 4, "data" : [ { "a" : "name", "b" : "Fetch" } ] }
In this case, the document within the array uses a
and b
fields instead of k
and v
.
Here’s what happens when we apply $arrayToObject
:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 4 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$arrayToObject requires an object with keys 'k' and 'v'. Missing either or both keys from: {a: \"name\", b: \"Fetch\"}", "code" : 40393, "codeName" : "Location40393" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1
In this case, the error states that $arrayToObject requires an object with keys 'k' and 'v'
.
Wrong Type
Similarly, if the argument is not even an array, an error occurs.
Suppose we have the following document:
{ "_id" : 5, "data" : "None" }
The data
field contains a string.
Here’s what happens when we apply $arrayToObject
to that document:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 5 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$arrayToObject requires an array input, found: string", "code" : 40386, "codeName" : "Location40386" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1
As the error states, $arrayToObject requires an array input
.
Null Values
Providing null
results in null
.
Suppose we have the following document:
{ "_id" : 6, "data" : null }
And we apply $arrayToObject
:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 6 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
{ "result" : null }
Missing Fields
If the field is missing, the result is null
.
Suppose we have the following document:
{ "_id" : 7 }
And we apply $arrayToObject
:
db.test.aggregate(
[
{ $match: { _id: { $in: [ 7 ] } } },
{ $project: {
_id: 0,
result: { $arrayToObject: "$data" } }
}
]
)
Result:
{ "result" : null }
Repeating Field Names
According to the MongoDB documentation, if the name of a field repeats in the array:
- Starting in 4.0.5,
$arrayToObject
uses the last value for that field. For 4.0.0-4.0.4, the value used depends on the driver. - Starting in 3.6.10,
$arrayToObject
uses the last value for that field. For 3.6.0-3.6.9, the value used depends on the driver. - Starting in 3.4.19,
$arrayToObject
uses the last value for that field. For 3.4.0-3.4.19, the value uses depends on the driver.