[SERVER-5916] In $group allow "$$ROOT" to expand to the whole document matched. Created: 23/May/12  Updated: 11/Jul/16  Resolved: 24/Sep/13

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

Type: Improvement Priority: Minor - P4
Reporter: Clifford Hammerschmidt Assignee: Mathias Stearn
Resolution: Done Votes: 5
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
related to SERVER-9840 Add variables, $$ROOT and $$CURRENT t... Closed
Backwards Compatibility: Fully Compatible
Participants:

 Description   

Allow:
db.coll.aggregate({$group:{_id:"$field", docs:{$push:"$$ROOT"}}});

Currently {$push:"$field"} is supported, which accumulates a field from the document. Adding support for "$$ROOT" to expand to the full document would seem to make sense then.



 Comments   
Comment by auto [ 24/Sep/13 ]

Author:

{u'username': u'RedBeard0531', u'name': u'Mathias Stearn', u'email': u'mathias@10gen.com'}

Message: SERVER-5916 Allow bare $$ROOT or $$CURRENT without subfields

This represents the whole variable (usually the full incoming document).
Branch: master
https://github.com/mongodb/mongo/commit/c30d78afdab3cfc95d1ece9f1e8680b7a2e1dc28

Comment by Mathias Stearn [ 17/Sep/13 ]

This should be handled by the $$ROOT variable introduced in SERVER-9840. I updated the example code to use the syntax from that ticket. I'm leaving this ticket open to add a test using this code.

Comment by Clifford Hammerschmidt [ 18/Dec/12 ]

I'm hoping this is a quick and easy one.

The main use would be for partitioning data into N sets.

Support in $project may make more sense since then you can do:

db.x.aggregate(
{ $project { computed: {$toLower:{ '$address.state_code' }}, doc: '$' } },
{ $group { _id: '$computed', docs:

{ $push: '$doc'}

} }
);

This would allow for grouping data in a collection based on a computation of the data without having to specify all the fields in both the $project and the $group statements. It also feel more inline with the idea of "schemaless" data as it doesn't force the documents processed to have anything other than the fields used in the computation present and makes the aggregation query more reusable in general.

Generated at Thu Feb 08 03:10:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.