MongoDB count() 与 countDocuments() [英] MongoDB count() versus countDocuments()

查看:504
本文介绍了MongoDB count() 与 countDocuments()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 MongoDB,更具体地说是 Mongoose.我最近在更新 mongo 和 mongoose 时消除了一些弃用警告.

我的一些查询可能如下所示:

const count = await Badge.find(query).count().exec()

我已将其更改为

const count = Badge.find(query).countDocuments().exec()

和中提琴贬低警告消失了.然而,阅读MongoDB的文档似乎应该是这样写的:

const count = Badge.countDocuments(query)

以上所有返回完全相同的东西.显然后两个是我想要的,因为 count() 的弃用.

后两个有什么区别,我应该更喜欢一个吗?

解决方案

db.collection.find 方法返回一个 cursor.游标计数的 cursor.count() 方法游标引用的文档数.这与 db.collection.count() 相同.

这两个方法(cursor.count()db.collection.count())从 MongoDB v4.0 开始被弃用.来自 文档:

<块引用>

与 4.0 特性兼容的 MongoDB 驱动程序弃用了它们的各自的游标和集合 count() API 支持新的 APIcountDocuments() 和估计的DocumentCount().对于特定的 API给定驱动程序的名称,请参阅驱动程序文档.

<块引用>

避免使用没有查询谓词的 db.collection.count() 方法由于没有查询谓词,该方法返回结果基于集合的元数据,这可能会导致近似计数.

db.collection.countDocuments(query) 返回与集合或视图的查询匹配的文档计数.这是您需要用来计算集合中文档数量的方法.

<块引用>

以上所有返回完全相同的东西.

是的,大多数时候.只有 countDocuments 返回文档的实际计数.其他方法根据集合的元数据返回计数.

如果您想使用db.collection.count,请将其与查询谓词一起使用,这将返回文档的确切计数(但是,请注意此方法已弃用).

I am using MongoDB and more specifically Mongoose. I've recently been getting rid of some deprecation warnings from updating mongo and mongoose.

Some of my queries might as shown:

const count = await Badge.find(query).count().exec()

I have since changed this to

const count = Badge.find(query).countDocuments().exec()

and viola the depracation warning is gone. However, reading the documentation at MongoDB it seems that it should be written like this:

const count = Badge.countDocuments(query)

All of the above return the exact same thing. Obviously the latter two are the ones I want because of the depracation of count().

What is the difference between the second two and should I prefer one over the other?

解决方案

The db.collection.find method returns a cursor. The cursor.count() method on the cursor counts the number of documents referenced by a cursor. This is same as the db.collection.count().

Both these methods (the cursor.count() and db.collection.count()) are deprecated as of MongoDB v4.0. From the documentation:

MongoDB drivers compatible with the 4.0 features deprecate their respective cursor and collection count() APIs in favor of new APIs for countDocuments() and estimatedDocumentCount(). For the specific API names for a given driver, see the driver documentation.

Avoid using the db.collection.count() method without a query predicate since without the query predicate, the method returns results based on the collection’s metadata, which may result in an approximate count.

db.collection.countDocuments(query) returns the count of documents that match the query for a collection or view. This is the method you need to use to count the number of documents in your collection.

All of the above return the exact same thing.

Yes, most of the times. Only, the countDocuments returns the actual count of the documents. The other methods return counts based upon the collection's meta data.

If you want to use db.collection.count, use it with a query predicate, and this will return the exact count of the documents (but, note that this method is deprecated).

这篇关于MongoDB count() 与 countDocuments()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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