Details
-
New Feature
-
Status: Closed
-
Major - P3
-
Resolution: Duplicate
-
None
-
None
-
None
-
Integration 17 (07/15/16)
Description
Syntax:
$transform: {
|
root: <expression evaluates to object>,
|
set: {<fieldname>: <arbitrary expression>}
|
}
|
Examples
// ===== Example #1 - Promoting a subfield to the top level =====
|
>db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
|
>db.example.aggregate([{$transform: {root: “$a1”}]);
|
{b: 1}
|
|
// ===== Example #2 - Creating a new field =====
|
>db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
|
>db.example.aggregate([{$transform: {set: {a3: 3}}}])
|
{_id: 0, a1: {b: 1}, a2: 2, a3: 3}
|
|
// ===== Example #3 - Creating new calculated fields =====
|
>db.example.insert({_id: 0, a1: {b: 1}, a2: 2})
|
>db.example.aggregate([{$transform: {set: {a3: {$gt: ["$a2", 3]}, a4: 4 }}}])
|
{_id: 0, a1: {b: 1}, a2: 2, a3: false, a4: 4}
|
|
// ===== Example #4 - Promoting a subfield to the top level and then creating new fields =====
|
>db.example.insert({_id: 0, a1: {b: 1}, a2: 2 })
|
>db.example.aggregate([{$transform: {root: "$a1", set: {c: 3}}}])
|
{b: 1, c: 3}
|
|
// ===== Example #5 - Error: using value of a field that doesn't exist anymore =====
|
>db.example.insert({_id: 0, a1: {b: 1}, a2: 2 })
|
>db.example.aggregate([{$transform: {root: "$a1", set: {c: "$a2"}}}])
|
Error
|
// Allowing this is a non-goal of this ticket.
|
This command will be another stage in the aggregation pipeline. Either the root or set fields (or both) must be present in the $transform. The transformation replaces the document with the root field first, and then sets the fields specified in set on the contents of the replaced document.
Attachments
Issue Links
- duplicates
-
SERVER-5781 Implement $addFields aggregation stage for using expression language to add new fields to a document
-
- Closed
-
-
SERVER-23313 Add a $replaceRoot stage which allows promoting a sub-document to the top level
-
- Closed
-