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.
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.
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.
It worth mentioning that the estimatedDocumentCount
output can be inaccurate as mentioned in the documentation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…