In MongoDB, the $objectToArray
aggregation pipeline operator converts a document to an array.
The array produced by $objectToArray
contains an element for each field/value pair in the original document. Each element is a document that contains a k
field and a v
field:
- The
k
field contains the field name in the original document. - The
v
field contains the value of the field in the original document.
Example
Suppose we have a collection called dogs
with the following document:
{ "_id" : 1, "name" : "Fetch", "specs" : { "height" : 400, "weight" : 55, "color" : "brown" } }
We can use the $objectToArray
operator to return the specs
field as an array:
db.dogs.aggregate(
[
{ $match: { _id: { $in: [ 1 ] } } },
{ $project: {
_id: 0,
result: { $objectToArray: "$specs" } }
}
]
).pretty()
Result:
{ "result" : [ { "k" : "height", "v" : 400 }, { "k" : "weight", "v" : 55 }, { "k" : "color", "v" : "brown" } ] }
Nested Documents
The $objectToArray
operator only applies to the top level field. It doesn’t recursively apply to any embedded documents.
Suppose we have a document like this:
{ "_id" : 2, "name" : "Wag", "specs" : { "height" : 50, "weight" : 5, "color" : { "eyes" : "brown", "coat" : "black" } } }
Here’s what happens when we apply $objectToArray
to that document:
db.dogs.aggregate(
[
{ $match: { _id: { $in: [ 2 ] } } },
{ $project: {
_id: 0,
result: { $objectToArray: "$specs" } }
}
]
).pretty()
Result:
{ "result" : [ { "k" : "height", "v" : 50 }, { "k" : "weight", "v" : 5 }, { "k" : "color", "v" : { "eyes" : "brown", "coat" : "black" } } ] }
In this case, the top level document is converted to the k
/v
format, but the embedded document remains the same as in the original document.
Wrong Type
The argument provided to $objectToArray
can be any valid expression as long as it resolves to a document object.
If the argument does not resolve to a document object, an error occurs.
Suppose we have the following document:
{ "_id" : 3, "name" : "Fetch", "specs" : "None" }
The specs
field contains a string.
Here’s what happens when we apply $objectToArray
to that document:
db.dogs.aggregate(
[
{ $match: { _id: { $in: [ 3 ] } } },
{ $project: {
_id: 0,
result: { $objectToArray: "$specs" } }
}
]
)
Result:
uncaught exception: Error: command failed: { "ok" : 0, "errmsg" : "$objectToArray requires a document input, found: string", "code" : 40390, "codeName" : "Location40390" } : 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, $objectToArray requires a document input
.
Null Values
Providing null
results in null
.
Suppose we have the following document:
{ "_id" : 4, "name" : "Fetch", "specs" : null }
And we apply $objectToArray
:
db.dogs.aggregate(
[
{ $match: { _id: { $in: [ 4 ] } } },
{ $project: {
_id: 0,
result: { $objectToArray: "$specs" } }
}
]
)
Result:
{ "result" : null }
Missing Fields
If the field is missing, the result is null
.
Suppose we have the following document:
{ "_id" : 5, "name" : "Fetch" }
And we apply $objectToArray
:
db.dogs.aggregate(
[
{ $match: { _id: { $in: [ 5 ] } } },
{ $project: {
_id: 0,
result: { $objectToArray: "$specs" } }
}
]
)
Result:
{ "result" : null }