-
Type: New Feature
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
In some cases, the documents of interest in the aggregation pipeline are nested inside an outer document. For example, if I had a schema like this:
{ _id: "user_id", first: "first name", last: "last name", addresses: [ {city: "New York", state: "NY", zip: "10036", address: "229 W 43rd"}, {city: "Palo Alto", state: "CA", zip: "94301", address: "100 Forest Ave"} ... ] }
Then I might have an aggregation pipeline which deals exclusively with the contents of 'addresses'. It's currently possible to do this, first by unwinding 'addresses', but this approach becomes annoying since all of the field names need to be prefixed with 'addresses.'. It would be useful to have some stage to promote the document inside 'addresses' to the top field. For example, something like this:
db.users.aggregate([ {$unwind: "$addresses"}, {$transform: {root: "$addresses"}} ]) {city: "New York", state: "NY", zip: "10036", address: "229 W 43rd"} {city: "Palo Alto", state: "CA", zip: "94301", address: "100 Forest Ave"}
Such a stage could also support other transformations, like those possible in $project, or some that are not possible, such as adding a field to a document (SERVER-5781).
- depends on
-
SERVER-23313 Add a $replaceRoot stage which allows promoting a sub-document to the top level
- Closed