The $arrayToObject operator converts an array of key-value pairs into a single object. It is useful when your data stores properties as an array (like metadata or settings) and you need a normal document shape for querying or display.
01
Array → Object
Reshape key-value data.
02
k / v Format
Use k and v fields.
03
Tuple Format
Use [key, value] pairs.
04
Field Arrays
Transform document fields.
05
Use Cases
Settings, specs, metadata.
06
Inverse
Pair with $objectToArray.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $arrayToObject operator takes an array where each element represents one key-value pair and returns a single object. For example, an array like [ { k: "color", v: "red" }, { k: "size", v: "L" } ] becomes { color: "red", size: "L" }.
💡
Beginner Tip
Think of $arrayToObject as the opposite of $objectToArray. One turns objects into arrays; the other turns arrays back into objects.
Foundation
📝 Syntax
The $arrayToObject operator takes one array expression:
mongosh
{ $arrayToObject: <array expression> }
Supported Array Element Formats
mongosh
// Format 1: k / v documents
[
{ k: "color", v: "red" },
{ k: "size", v: "L" }
]
// Format 2: two-element arrays
[
[ "color", "red" ],
[ "size", "L" ]
]
Syntax Rules
Each key (k or first tuple element) must be a string.
Values (v or second tuple element) can be any BSON type.
Use inside stages like $project, $addFields, or $set.
Duplicate keys: the last value wins.
An empty input array returns an empty object {}.
💡 Input → Output Example
Input: [ { k: "a", v: 1 }, { k: "b", v: 2 } ]
Output: { a: 1, b: 2 }
Input: [ ["x", true], ["y", false] ]
Output: { x: true, y: false }
Cheat Sheet
⚡ Quick Reference
Question
Answer
Operator type
Aggregation expression operator (array transform)
Syntax
{ $arrayToObject: <array> }
Key format
{ k, v } or [ key, value ]
Key type
String (required)
Inverse operator
$objectToArray
k / v
{
k: "a",
v: 1
}
Document format
Tuple
[
"a",
1
]
Array format
Field
{
$arrayToObject: "$specs"
}
From document field
Empty
{
$arrayToObject: []
}
Returns {}
Hands-On
Examples Gallery
Convert product specs, user settings, and literal key-value arrays into easy-to-read objects.
📚 Key-Value Arrays
Transform stored metadata arrays into objects with $project.
Sample Input Documents
Suppose you have a products collection with a specs array in k / v format:
Be careful when building arrays dynamically. Duplicate keys silently keep only the last value.
Applications
🚀 Use Cases
Product specifications — convert attribute arrays into flat objects for display or export.
User preferences — flatten settings arrays into readable fields.
Configuration data — reshape key-value config arrays into document form.
ETL pipelines — normalize array-based metadata before loading into reports or APIs.
🧠 How $arrayToObject Works
1
MongoDB reads the array
The pipeline evaluates the array — a field like "$specs" or a literal array of pairs.
Input
2
Each pair becomes a property
MongoDB extracts the key string and value from each element ({ k, v } or [ key, value ]).
Transform
3
A single object is built
All pairs are merged into one object. Duplicate keys keep the last value.
Merge
=
🗃
Document-shaped output
Access fields with dot notation like $specsObject.color.
Wrap Up
Conclusion
The $arrayToObject operator is a practical reshaping tool when your data stores properties as key-value arrays. It supports both { k, v } documents and [ key, value ] tuples, and pairs naturally with $objectToArray for round-trip conversions.
For beginners, remember: keys must be strings, duplicate keys keep the last value, and an empty array becomes {}.