The $objectToArray operator converts an object into an array of { k, v } pairs. Use it when you need to iterate over dynamic keys, flatten metadata, or reshape nested objects for reporting.
01
Object → Array
Split into k/v pairs.
02
k / v Output
Standard pair format.
03
$unwind Ready
One row per key.
04
Field Objects
Transform document fields.
05
Use Cases
Settings, attributes.
06
Inverse
Pair with $arrayToObject.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $objectToArray operator takes an object and returns an array where each property becomes one element. For example, { theme: "dark", lang: "en" } becomes [ { k: "theme", v: "dark" }, { k: "lang", v: "en" } ].
This is especially useful for objects with dynamic or unknown keys — user preferences, product attributes, or configuration maps — where you cannot hard-code field names.
💡
Beginner Tip
Think of $objectToArray as the opposite of $arrayToObject. Convert to an array when you need to loop, filter, or sort individual key-value pairs with $unwind.
Foundation
📝 Syntax
The $objectToArray operator takes one object expression:
After converting and unwinding, $match filters on pairs.k and pairs.v. This works for any dynamic key without knowing field names in advance at schema design time.
Example 4 — Round Trip with $arrayToObject
Convert to array, modify, and convert back to an object:
Products with different attribute keys (weight, color, size, etc.) can all be normalized into a consistent three-column format for CSV export or BI tools.
Applications
🚀 Use Cases
Dynamic key iteration — process objects when field names are not fixed at design time.
Settings flattening — convert preference objects into rows for reporting.
Attribute normalization — reshape product specs or metadata for export.
Pipeline transforms — modify key-value pairs with $map before converting back with $arrayToObject.
🧠 How $objectToArray Works
1
MongoDB reads the object
The pipeline evaluates the object expression — a field like "$settings" or a literal.
Input
2
Each property becomes a pair
MongoDB extracts every key as k (string) and every value as v (any type).
Transform
3
An array is returned
All pairs are collected into one array, ready for $unwind, $filter, or $map.
Output
=
📦
Iterable key-value data
Access .k and .v after $unwind for flexible reporting.
Wrap Up
Conclusion
The $objectToArray operator converts objects into arrays of { k, v } pairs in MongoDB aggregation pipelines. It is essential when you need to iterate, filter, or reshape data with dynamic keys.
Pair it with $unwind for row-per-key output and $arrayToObject for round-trip conversions. Next in the series: $or.
Use $objectToArray for dynamic or unknown object keys
Follow with $unwind when you need one document per pair
Filter on .k and .v after unwinding
Pair with $arrayToObject for round-trip transforms
Handle empty objects ({} → []) in downstream stages
❌ Don’t
Use when you already know all field names (direct projection is simpler)
Forget that null input returns null, not []
Assume key order in the output array matches insertion order in all cases
Nest $objectToArray without planning for array size growth
Confuse output { k, v } format with [ key, value ] tuples ($arrayToObject accepts both)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $objectToArray
Use these points when converting objects in aggregation pipelines.
5
Core concepts
📦01
Object to Array
Properties → k/v pairs.
Purpose
📝02
One Argument
Object expression.
Syntax
🛠03
k / v Output
Standard pair shape.
Format
🔁04
+ $unwind
One row per key.
Pattern
🔄05
Inverse
$arrayToObject back.
Pairing
❓ Frequently Asked Questions
$objectToArray converts an object into an array of key-value pairs. Each property becomes { k: "keyName", v: value }. It is the inverse of $arrayToObject.
The syntax is { $objectToArray: <object expression> }. Pass a field path like "$settings" or a literal object expression.
An array of documents with k (string key) and v (value) fields. For example, { theme: "dark", lang: "en" } becomes [{ k: "theme", v: "dark" }, { k: "lang", v: "en" }].
Yes. Convert the object to an array, then $unwind the array 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. The two operators are inverses.