简体   繁体   English

C# MongoDbDriver 迁移过时计数替换方法

[英]C# MongoDbDriver migrating obsolete Count to replacement methods

For some time we've been getting warnings in our code:一段时间以来,我们的代码中一直收到警告:

Warning CS0618 'IMongoCollection.Count(FilterDefinition, CountOptions, CancellationToken)' is obsolete: 'Use CountDocuments or EstimatedDocumentCount instead.'警告 CS0618 'IMongoCollection.Count(FilterDefinition, CountOptions, CancellationToken)' 已过时:'改用 CountDocuments 或 EstimatedDocumentCount。

So we want to "update".所以我们要“更新”。 However, we're running into performance problems.但是,我们遇到了性能问题。 Which seem to have already reported in 2018 on Jira (mongoDb driver issue tracker) .这似乎已经在 2018 年在Jira (mongoDb driver issue tracker)上报道过。

I did some tests myself and found the following for a small collection,which already shows the differences:我自己做了一些测试,并为一个小集合找到了以下内容,这已经显示了差异:

  • Count returns 6600 and took 12 ms.计数返回 6600 并花费了 12 毫秒。
  • CountDocuments returns 6600 and took 25 ms. CountDocuments 返回 6600 并花费了 25 毫秒。
  • EstimatedDocumentCount returns 6600 and took 2 ms. EstimatedDocumentCount 返回 6600,耗时 2 毫秒。

But then for a big collection:但是对于一个大集合:

  • Count returns 2721199 and took 12 ms. Count 返回 2721199,耗时 12 毫秒。
  • CountDocuments returns 2721199 and took 196406 ms. CountDocuments 返回 2721199 并花费了 196406 毫秒。
  • EstimatedDocumentCount returns 2721199 and took 4 ms. EstimatedDocumentCount 返回 2721199,耗时 4 毫秒。

That's 196 seconds!那是196秒! 16367x slower!16367 倍 Totally unacceptable...完全不能接受...

One could argue "Then just use EstimatedDocumentCount ".有人可能会争辩说“然后只使用EstimatedDocumentCount ”。 However, EstimatedDocumentCount doesn't offer the same interface: ie you cannot filter/there's no filter parameter or option.但是, EstimatedDocumentCount不提供相同的接口:即您不能过滤/没有过滤参数或选项。

And if you would try the something like collection.Find(...).EstimatedDocumentCount() that doesn't work, as IFindFluent doesn't offer an EstimatedDocumentCount method.如果你想尝试像collection.Find(...).EstimatedDocumentCount()这样不起作用的东西,因为IFindFluent不提供EstimatedDocumentCount方法。 And we do want to filter often.我们确实想经常过滤。

What's up with this?这是怎么回事? How can they deprecate a functionality and not replace it with something good?他们怎么能弃用一个功能而不用好的东西代替它呢?

Does anybody know how to fix this?有人知道如何解决这个问题吗?

edit: Like @Joe suggested, I added a filter (didn't do that before).编辑:就像@Joe 建议的那样,我添加了一个过滤器(之前没有这样做)。 Now I get different results:现在我得到不同的结果:

  • Count returns 2060277 and took ~434 ms.计数返回 2060277 并花费了约 434 毫秒。
  • CountDocuments returns 2060277 and took ~575 ms. CountDocuments 返回 2060277,耗时约 575 毫秒。
  • EstimateDocumentCount is not an option when filtering过滤时 EstimateDocumentCount 不是一个选项

Weird.诡异的。 But anyhow, CountDocuments is still 25% slower, which is significant.但无论如何,CountDocuments 仍然慢了 25%,这很重要。 Can this be fixed?这可以解决吗? (The query is already using an index) (查询已经在使用索引)

Previously, calling Count implicitly (and silently) used the EstimatedDocumentCount behavior when no filter was provided, and the CountDocuments behavior when one was provided.以前,在未提供过滤器时隐式(且无声地)调用Count使用EstimatedDocumentCount行为,并在提供过滤器时使用CountDocuments行为。

It is called "Estimated" because all it does is retrieve the document count from the collection metadata.它被称为“估计”,因为它所做的只是从集合元数据中检索文档计数。 That value is usually correct, but there are a number of things that can cause that metadata value to drift from the actual count.该值通常是正确的,但有许多因素会导致元数据值偏离实际计数。

This was a source of confusion in the past, and MongoDB apparently got tired of responding "works as designed" and recommending the use of a filter that would match all documents to get the accurate count, so they split Count into two separate functions that explicitly use only one of those behaviors.这在过去是一个混乱的根源,MongoDB 显然厌倦了响应“按设计工作”并建议使用匹配所有文档以获得准确计数的过滤器,因此他们将Count拆分为两个单独的函数,明确只使用其中一种行为。

This means that you now have a choice.这意味着你现在有一个选择。 If you want to get the count of all documents in the collection, you can call CountDocuments to get the up to the minute completely accurate answer, or you can call EstimatedDocumentCount to really quickly pull the hopefully correct metadata.如果您想获取集合中所有文档的计数,您可以调用CountDocuments来获得最新的完全准确的答案,或者您可以调用EstimatedDocumentCount来真正快速地提取希望正确的元数据。

Since getting the count from metadata is literally just retrieving a single integer, it cannot support filtering.由于从元数据中获取计数实际上只是检索单个 integer,因此它不支持过滤。

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

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