[SERVER-24779] Add a $transform aggregation stage Created: 24/Jun/16  Updated: 27/Jun/16  Resolved: 27/Jun/16

Status: Closed
Project: Core Server
Component/s: Aggregation Framework
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Carly Robison Assignee: Carly Robison
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-5781 Implement $addFields aggregation stag... Closed
duplicates SERVER-23313 Add a $replaceRoot stage which allows... Closed
Sprint: Integration 17 (07/15/16)
Participants:

 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.



 Comments   
Comment by Carly Robison [ 27/Jun/16 ]

Duplicate of SERVER-23313 and SERVER-5781 – actual functionality will be provided separately.

Generated at Thu Feb 08 04:07:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.