简体   繁体   English

MongoDB C# 驱动程序 - CountDocuments 错误地返回错误值

[英]MongoDB C# Driver - CountDocuments erronously returns wrong value

Ok, so this is a bit tricky.好的,所以这有点棘手。

I have this block我有这个块

        var collResult = _coll.Find(cnFilter & statusFilter & startFilter & endFilter & textFilter);
        var findTask = Task.Factory.StartNew(() => {
            return collResult.SortByDescending(i => i.dim_InvoiceDate_key).Skip(skip).Limit(take);
        });
        var countTask = Task.Factory.StartNew(() => {
            return collResult.CountDocuments();
        });
        await Task.WhenAll(findTask, countTask);

the CountDocuments most of the time returns an accurate number representing the filtered query without Skip and Limit. CountDocuments 大多数时候返回一个准确的数字,表示没有跳过和限制的过滤查询。 But occasionally, when it has to count in excess of 25k or so, it returns the same count as the findTask result contains.但偶尔,当它的计数超过 25k 左右时,它会返回与 findTask 结果包含的相同计数。 (50 in most cases) (大多数情况下为 50 个)

Trying to use the old _coll.Count() and I get obsolete errors thrown at me, is there anything I can do for this?尝试使用旧的 _coll.Count() 并且我收到过时的错误,我能为此做些什么吗?

It seems though that if the findTask finishes before Count even gets time to start (due to concurrency or whatever), the Limit set inside findTask is leaking through the shared Find() and CountDocuments() is then thinking its supposed to count that.似乎如果 findTask 在 Count 甚至有时间开始之前完成(由于并发或其他原因),findTask 中设置的 Limit 正在通过共享的 Find() 泄漏,而 CountDocuments() 则认为它应该计算它。 It wasnt even enough that I put the task before the findTask so that it should have a sporting chance to finish before, I had to create it's own Find-pipeline so that it didnt leak over.将任务放在 findTask 之前甚至还不够,这样它应该有机会在之前完成,我必须创建它自己的 Find-pipeline 以便它不会泄漏。

            var findResult = _coll.Find(cnFilter & orderDateFilter & deliveryDateFilter & statusFilter & keywordFilter);
            var countResult = _coll.Find(cnFilter & orderDateFilter & deliveryDateFilter & statusFilter & keywordFilter).CountDocumentsAsync();

and then I use the countResult task seperately然后我分别使用 countResult 任务

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM