2 / 2
Jun 2024

I have a following piece of code in C#:

IAsyncCursor<Class>? dataCursor = await _mongoCollection.Aggregate() .Match(s => s.Id == 1) .Group( s => new { s.Id }, g => new Class( g.Key.Id, g.Count(), g.Max(s => s.Value), g.Average(s => s.Value), g.Min(s => s.Value), g.Sum(s => s.Value), g.Sum(s => s.Value * s.Value))) .ToCursorAsync(); while (await dataCursor.MoveNextAsync()) { foreach (Class? data in dataCursor.Current) { yield return data; } }

How can I be sure that the aggregation is executed on Database side?

2 months later

Hi @Bartosz_Kuriata ,

Welcome to the MongoDB Community Forums! Everything that you have above in the Fluent Aggregate Query will be executed server side. If you use a ToList() or ToListAsync() at the end instead then it would be executed client side. You can even verify the MQL to be executed on the server if you append a ToString() at the end instead of ToCursorAsync() . Hope that helps.

Thanks,

Rishit.