且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MongoDB中count()和find().count()之间的区别

更新时间:2023-12-04 11:35:34

db.collection.count()cursor.count()只是分片群集中的错误.

db.collection.count() and cursor.count() are simply wrappers around the count command thus running db.collection.count() and cursor.count() with/without the same same will return the same query argument, will return the same the result. However the count result can be inaccurate in sharded cluster.

与4.0功能兼容的MongoDB驱动程序已弃用 各自的游标和集合count()API,而推荐使用新的API countDocuments()和estimatedDocumentCount().对于特定的API 给定驱动程序的名称,请参阅驱动程序文档.

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.

db.collection.countDocuments 方法在内部使用聚合查询返回文档计数,而 db.collection.estimatedDocumentCount/ 返回基于元数据的文档计数

The db.collection.countDocuments method internally uses an aggregation query to return the document count while db.collection.estimatedDocumentCount/ returns documents count based on metadata.

值得一提的是,如estimatedDocumentCount输出可能不准确. ="noreferrer">文档.

It worth mentioning that the estimatedDocumentCount output can be inaccurate as mentioned in the documentation.