$objectToArray is a MongoDB aggregation expression that turns an object into an array of{ k, v } pairs—one element per top-level field. It is the inverse of $arrayToObject.
Remember
$objectToArray = object → [ { k, v }, ... ]
{ $objectToArray: <objectExpression> }
input:
{ theme: "dark", lang: "en" }
output:
[
{ k: "theme", v: "dark" },
{ k: "lang", v: "en" }
]
k → field name (string)
v → field value
top-level only (not recursive)
null / missing → null
Optionally $unwind the array for one document per field.
Piece
Required?
Role
Object expression
Yes
Document / embedded object to convert
Result
—
Array of { k, v } documents
Pseudocode
Pseudocode
obj = evaluate(objectExpression)
if obj is null or missing:
result = null
else:
result = []
for each top-level field in obj:
append { k: fieldName, v: fieldValue }
emit { ..., field: result }
Reference
Cheat Sheet
Keep these facts handy while converting objects to arrays.
We use one user document with a settings object. First we store it, then we convert to pairs, then we unwind to one document per setting. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine one user named Asha with settings theme: dark and lang: en.
Unwind the array.$unwind makes one document per pair.
Lift k and v. Project key and value for a flat, readable shape.
Same _id. Both rows still belong to Asha.
Edge Cases & Pitfalls
Check these before using $objectToArray in production pipelines.
depth
Not recursive
Nested objects stay inside v. Convert nested fields with another $objectToArray if you need deeper pairs.
type
Input must be an object
Arrays, numbers, and strings are not valid inputs. Pass a document or embedded object field.
null
Null or missing → null
Missing fields return null, not []. Guard before $unwind if needed.
order
Field order can vary
Do not rely on a fixed array order for business logic. Filter or sort by k when order matters.
Analysis
Performance Notes
Approach
Cost
Best for
$objectToArray alone
Cheap per document
Small embedded objects
+ $unwind
More output docs
Per-key reporting / grouping
Huge objects
Large arrays
Prefer leaner schemas when possible
Converting a small settings-style object is inexpensive. Unwinding multiplies document count—filter early when you only need some keys.
Remember
Key Takeaways
Role: object → array of { k, v } pairs.
Syntax:{ $objectToArray: "$field" }.
Shape:k = key, v = value (top-level only).
Inverse:$arrayToObject rebuilds the object.
One line:$objectToArray answers “what are this object’s fields as a list of key/value pairs?”
Frequently Asked Questions
$objectToArray converts an object into an array of key-value pairs. Each field becomes { k: "fieldName", v: value }. It is the inverse of $arrayToObject. Use it inside aggregation stages such as $project and $addFields.
The syntax is { $objectToArray: <objectExpression> }. Pass a field path like "$settings" or any expression that resolves to a document object.
An array of documents with k (string key) and v (value). For example, { theme: "dark", lang: "en" } becomes [ { k: "theme", v: "dark" }, { k: "lang", v: "en" } ].
No. It only converts top-level fields of the argument. Nested objects appear as the v value unchanged.
Yes. Convert the object to an array, then $unwind to get one document per key-value pair. Access the key with pairs.k and the value with pairs.v.
$arrayToObject converts an array of { k, v } pairs (or [key, value] tuples) back into a single object.