Mongo C # driver 2.0 - Find a counter without receiving documents

The general counting request will execute

int count = collection.Find(filter).Count();

Now that all the records are loaded according to the filter, so let's point out that I have 1 million records, and of these 0.5 million correspond to my filter, so I will have a collection filled with just 0.5 documents. This is good enough if you want documents, but what if you just want to know the bill and do not need documents, for the sake of memory.

Can i do something like this

int count = collection.Find(filter).SetLimit(1).Count();

This gives me the same meaning as the first expression, but I hope that the memory will not be used as the first expression, help me find out the correct way to find the "account" without loading all the documents. Thanks.

+4
source share
1 answer

You need to use an explicit method CountAsync, not Find:

long result = await collection.CountAsync(Builders<Hamster>.Filter.Eq(_ => _.Name, "bar"));
+8
source

Source: https://habr.com/ru/post/1606941/


All Articles