[CSHARP-3214] Missing extension methods for AsyncCursorSource and IAsyncCursor Created: 24/Sep/20  Updated: 27/Oct/23  Resolved: 28/Sep/20

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

Type: New Feature Priority: Major - P3
Reporter: Mohamad Javad Ebrahimi Assignee: Robert Stam
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Why classes `IAsyncCursorSourceExtensions` and `IAsyncCursorExtensions` have no extension methods for `CountAsync`, `AverageAsync`, `MinAsync`, `MaxAsync` `SumAsync`, etc.?
Would it be better to add them?
If yes, I can add



 Comments   
Comment by Robert Stam [ 28/Sep/20 ]

Methods like `CountAsync`, `AverageAsync`, `MinAsync`, `MaxAsync` , `SumAsync` etc... are LINQ methods that are intended to execute server side. You want to use these methods as part of a LINQ query.

For example, the following LINQ query executes entirely server side and only the count is returned to the client:

var count = collection.AsQueryable().Where(x => x.Age == 21).Count();

You can use existing driver APIs to count the matching documents client side if you wish (though I would not recommend that):

var cursor = collection.FindSync(x => x.Age == 21);
var count = cursor.ToEnumerable().Count();

But note that:

  • This transmits ALL matching documents to the client and the count is computed client side
  • The entire documents are transmitted to the client even though the client only intends to count them
  • You use `ToEnumerable` on the cursor to signal the transition from server side to client side execution

I can't think of any case where you would want to do the count client side rather than server side.

 

Comment by Esha Bhargava [ 28/Sep/20 ]

mj.ebrahimi72@gmail.com Thanks for reporting this! We'll have a look at this soon and get back to you.

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