C# driver - CountAsync vs. Find/CountAsync

1,222 views
Skip to first unread message

Thomas

unread,
Dec 30, 2016, 8:49:50 PM12/30/16
to mongodb-user
What is the difference between using CountAsync(Filter) and Find(Filter).CountAsync?

In general, it would be a good idea to have some documentation of the C# driver to know what to use and what not to use because it seems like there is a lot of overlay and all the answers on stack overflow seem to relate to different versions of the driver with 'do this, not that', only to be contradicted by another post which could be related to another version, etc.

My team, and myself, end up using calls 'that work' without having the slightest idea what is the right way to do thing vs. what is working but not advised, or what side effects might be since there is no documentation about this subject on the C# driver. It is lacking a formal documentation which is not made of samples.

This thread is a perfect example of everyone seems to be confused about the C# api since information comes by bits and pieces: http://stackoverflow.com/questions/31171451/getting-a-single-object-from-mongodb-in-c-sharp

Thomas

unread,
Dec 30, 2016, 8:54:32 PM12/30/16
to mongodb-user
SImilarly, I don't understand the differences between: FirstAsync, SingleAsync

Robert Stam

unread,
Jan 3, 2017, 9:02:44 AM1/3/17
to mongod...@googlegroups.com
There is no difference between collection.Count(filter) and collection.Find(filter).Count(). The latter is simply a convenience method of the fluent find API which allows you to express the parameters to Count in the same way as you would to a fluent Find. The implementation of collection.Find(filter).Count() calls collection.Count().

The documentation for the .NET driver is here:


Regarding your second post, First and Single are just like the same methods in LINQ, except that they are terminating methods of the fluent find API. First returns the first matching document or null if none matches. Single returns the one and only matching document or throws an exception if zero or more than one documents match.

On Fri, Dec 30, 2016 at 7:54 PM, Thomas <thomas...@gmail.com> wrote:
SImilarly, I don't understand the differences between: FirstAsync, SingleAsync

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/7eaada50-a23a-44c8-b2e2-6b063d469e99%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages