蒙戈C#2.0的驱动程序 - 找到不取的文件数 [英] Mongo C# driver 2.0 - Find count without fetching documents

查看:133
本文介绍了蒙戈C#2.0的驱动程序 - 找到不取的文件数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般数查询会做一个

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

现在是加载所有的记录按照过滤器,所以让我们说,我有100万条记录和出这0.5万次火柴我的过滤器,所以我已经装满0.5文档集合。如果你想要的文件,但如果你只是想知道计数,而不是真正需要的文件,对内存的缘故这是什么就好了。

Now that loads all the records as per the filter, so lets says I have 1 million records and out of those 0.5 million match my filter, so I'll have collection already filled with 0.5 documents. This is good enough if you want the documents, but what if you just want to know the count and not really need the documents, for memory sake.

我能做些什么像这样

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

这给了我同样的次数作为第一个表情,但我希望记忆不会用作第一个表达式,帮助我知道正确的方式找到了计数,而无需加载所有文件。谢谢你。

This gives me the same count as the first expression, but I hope that the memory will not utilized as the first expression, help me to know the correct way to find the "count" without loading all documents. Thanks.

推荐答案

您需要使用显式的 CountAsync 方法,而不是查找

You need to use the explicit CountAsync method and not Find:

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

这篇关于蒙戈C#2.0的驱动程序 - 找到不取的文件数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆