[SERVER-27614] provide aggregation expression to remove field: $$REMOVE Created: 09/Jan/17  Updated: 21/Jan/18  Resolved: 13/Mar/17

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

Type: Improvement Priority: Major - P3
Reporter: Asya Kamsky Assignee: David Storch
Resolution: Done Votes: 3
Labels: expression, neweng, usability
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Documented
is documented by DOCS-10022 Docs for SERVER-27614: provide aggreg... Closed
Problem/Incident
causes SERVER-32819 Tests in jstests/aggregation/variable... Closed
Backwards Compatibility: Fully Compatible
Backport Requested:
v3.4
Sprint: Query 2017-03-27
Participants:

 Description   

The $project stage supports both inclusion and exclusion based projections. However, there is no way to conditionally exclude a field based on the result of some computation. In order to support this use case, we will add a new system variable REMOVE. When a field f is set to the value of an expression, and that expression evaluates to $$REMOVE, f is excluded from the resulting document.

Suppose, for example, that you wanted to include the field middleInitial, unless its value was the empty string. This could be achieved as shown below:

> db.names.drop();
> db.names.insert({last: "Smith", middleInitial: "H", first: "John"});
> db.names.insert({last: "Doe", middleInitial: "", first: "Jane"});
 
// Here we have removed the middleInitial field, but only in those documents where it contained the empty string.
> db.names.aggregate([{$project: {
    _id: 0,
    last: 1,
    middleInitial: {$cond: {if: {$eq: ["", "$middleInitial"]}, then: "$$REMOVE", else: "$middleInitial"}},
    first: 1
}}]);
{ "last" : "Smith", "first" : "John", "middleInitial" : "H" }
{ "last" : "Doe", "first" : "Jane" }



 Comments   
Comment by Githook User [ 14/Mar/17 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-27614 fix linker error on OS X
Branch: master
https://github.com/mongodb/mongo/commit/08e19cba750f74ae1a4ebdb0d97c3d63182e2bc1

Comment by Githook User [ 13/Mar/17 ]

Author:

{u'username': u'dstorch', u'name': u'David Storch', u'email': u'david.storch@10gen.com'}

Message: SERVER-27614 add $$REMOVE agg system variable
Branch: master
https://github.com/mongodb/mongo/commit/2f367a1e6aafa0e6bdf58f0564d3dcd7b279733f

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