[CSHARP-1173] Aggregation Framework needs to support $addToSet and $push inside a Group expression tree Created: 28/Jan/15  Updated: 02/Apr/15  Resolved: 28/Jan/15

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 2.0

Type: New Feature Priority: Major - P3
Reporter: Craig Wilson Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Currently, the only 2 accumulators we do not support are $push and $addToSet.

We could implement these in a simple manner with the following syntax.

For $push:

{ $group: {_id: "$State", Cities: { $push: "$City" } } }

.Group(x => x.State, g => new 
{ 
  _id = g.Key,
  Cities = new List<string>(g.Select(x => x.City))
}

And for $addToSet:

{ $group: {_id: "$State", Cities: { $addToSet: "$City" } } }

.Group(x => x.State, g => new
{ 
  _id = g.Key,
  Cities = new HashSet<string>(g.Select(x => x.City))
}



 Comments   
Comment by Githook User [ 28/Jan/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1173: added support for $addToSet into AggregateFluent's Group method.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/6acf8e2263579c4526d6c100c250b242cd18056b

Comment by Githook User [ 28/Jan/15 ]

Author:

{u'username': u'craiggwilson', u'name': u'Craig Wilson', u'email': u'craiggwilson@gmail.com'}

Message: CSHARP-1173: added support for $push into AggregateFluent's Group method.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/575b45221db4c36285382b4fbf6f825afbd38aa0

Comment by Craig Wilson [ 28/Jan/15 ]

An alternative for the first one is that if it's just g.Select(x => x.City), then we know use a $push implicitly without any extra direction. It would certainly lower the error rate by just doing what they think they want anyway.

For $push:

 { $group: {_id: "$State", Cities: { $push: "$City" } } }

.Group(x => x.State, g => new 
{ 
  _id = g.Key,
  Cities = g.Select(x => x.City)
}

Generated at Wed Feb 07 21:38:52 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.