本文介绍了MongoDB 的估计计数查询有多准确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方 MongoDB 驱动程序提供了计数"和估计文档计数"API,据我所知,前一个命令占用大量内存,因此建议在需要的情况下使用后者.

The official MongoDB driver offers a 'count' and 'estimated document count' API, as far as I know the former command is highly memory intensive so it's recommended to use the latter in situations that require it.

但是这个估计的文档数量有多准确?计数在生产环境中是否可信,或者在需要绝对准确度时使用推荐的计数 API?

But how accurate is this estimated document count? Can the count be trusted in a Production environment, or is using the count API recommended when absolute accuracy is needed?

推荐答案

比较这两者,对我来说很难想象出你想要使用 countDocuments() 的场景,当estimatedDocumentCount() 是一个选项.

Comparing the two, to me it's very difficult to conjure up a scenario in which you'd want to use countDocuments() when estimatedDocumentCount() was an option.

也就是说,estimatedDocumentCount() 的等效形式是 countDocuments({}),即一个空的查询过滤器.第一个函数的代价是O(1);第二个是O(N),如果N非常大,成本会高得令人望而却步.

That is, the equivalent form of estimatedDocumentCount() is countDocuments({}), i.e., an empty query filter. The cost of the first function is O(1); the second is O(N), and if N is very large, the cost will be prohibitive.

两者都返回一个计数,在部署了 Mongo 的场景中,该计数可能非常短暂,即,随着集合的变化,它在您拥有它的那一刻是不准确的.

Both return a count, which, in a scenario in which Mongo has been deployed, is likely to be quite ephemeral, i.e., it's inaccurate the moment you have it, as the collection changes.

这篇关于MongoDB 的估计计数查询有多准确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 20:34