Details
-
New Feature
-
Status: Closed
-
Major - P3
-
Resolution: Done
-
None
-
None
-
Fully Compatible
-
v3.4
-
Query 2017-03-27
Description
In many cases, it would be useful to construct an object from an array of key/value pairs. The new expression should be designed to work with the expression requested in SERVER-18794. For example, we could add an $arrayToObject expression, which could do the following:
db.foo.drop();
|
db.foo.insert({
|
myArray: [ ["price", 200], ["item", "mango"] ], |
document: {x: 1, y: 2, z: 2},
|
docs: [
|
{email: "charlie", count: 4, blah: "foo"}, |
{email: "eliot", count: 500, blah: "bar"}, |
]
|
});
|
db.foo.aggregate([{
|
$project: {
|
convertedFrom2dArray: {$arrayToObject: "$myArray"}, |
noOp: {$arrayToObject: {$objectToArray: "$document"}}, |
aggregated: {$arrayToObject: {$zip: ["$docs.email", |
"$docs.count"]}}} |
}
|
}]);
|
|
// Output:
|
{
|
_id: xxx,
|
convertedFrom2dArray: {"price": 200, "item": "mango"}, |
noOp: {x: 1, y: 2, z: 2},
|
aggregated: {charlie: 4, eliot: 500},
|
}
|
The expression should accept an array of pairs either represented as
- an array of two-element arrays (like [ ["price", 200], ["item", "mango"] ] above), or
- an array of two-field objects with key names k and v.
In the latter case, the expression would permit conversion of [{k: "price", v: 200}, {k: "item", v: "mango"}] to {price: 200, item: "mango"}.
Attachments
Issue Links
- is depended on by
-
CSHARP-1940 Add an expression to convert an array of pairs to an object
-
- Backlog
-
- is duplicated by
-
SERVER-5947 Add ability to project key names as values and values as key names
-
- Closed
-
-
SERVER-23729 Crafting subdocuments with the aggregation framework
-
- Closed
-
- related to
-
DOCS-10459 The documentation for $objectToArray and $arrayToObject is missing
-
- Closed
-
-
DRIVERS-358 Aggregation Framework Support for 3.6
-
- Closed
-
-
SERVER-18794 Add an aggregation expression to convert an object to an array of key, value pairs
-
- Closed
-
- links to