AggregateAsync: Code sample?

2,285 views
Skip to first unread message

Mark Shaw

unread,
Apr 28, 2015, 6:45:30 PM4/28/15
to mongod...@googlegroups.com
Ok. I give up and raise the white flag. I am trying to upgrade to the c# driver 2.0 from 1.10 and for the life of me I can't figure out how to port all my aggregation code.

Really need to improve the documentation... the API is far from intuitive.

How does one convert this method?

/// <summary>
/// Runs an aggregation over the mongodb
/// </summary>
/// <param name="args">the aggregation pipeline</param>
/// <returns>the aggregate data</returns>
public IEnumerable<BsonDocument> Aggregate(IEnumerable<BsonDocument> pipeline)
{
 // Get our mongo db objects
 var rmu = DB()
  .GetServer()
  .GetDatabase(Database)
  .GetCollection(Collection);
 // Execute
 var cmd = new AggregateArgs() { Pipeline = pipeline, OutputMode = AggregateOutputMode.Cursor, AllowDiskUse = true };
 var results = rmu.Aggregate(cmd);
 return results;
}








        }





Craig Wilson

unread,
Apr 28, 2015, 7:30:41 PM4/28/15
to mongod...@googlegroups.com
Here are the docs: http://mongodb.github.io/mongo-csharp-driver/2.0/reference/driver/crud/reading/#aggregation

However, since you already have an enumerable of BsonDocument's representing your stages, you can pass them in as a list.

var cursor = await collection.AggregateAsync(stages.ToList(), new AggregateOptions { AllowDiskUse = true });

I'll make a note to add in an example like this to the documentation.

Craig

Mark Shaw

unread,
Apr 29, 2015, 9:13:29 AM4/29/15
to mongod...@googlegroups.com
Ugg... this works but while the async pattern is nifty, you essentially broke your cursor capability unless your entire application (think huge enterprise app) is re-written utilizing the async pattern.

I would suggest that someone promotes this notion of 'legacy' API because async/await is a drastic application architecture change and there is nothing fundamentally wrong with the 'legacy' approach.

BTW: you need to provide the type for your code to function. Thanks.

var cursor = await col.AggregateAsync<BsonDocument>(pipeline.ToList(), options);

Craig Wilson

unread,
Apr 29, 2015, 10:33:28 AM4/29/15
to mongod...@googlegroups.com
Hi Mark,

While we've called it legacy because we aren't going to be developing features for it, it is still fully supported and will get bug fixes, etc.... I was under the impression you were moving to async so I provided the async version of your method.  We can still introduce a synchronous API if there is enough demand. When we polled the community, a vast majority responded indicating they wanted async and were fine with it being solely async. If you'd like us to introduce a sync API, then please file a ticket at jira.mongodb.org under the CSHARP project.

Craig
Reply all
Reply to author
Forward
0 new messages