Python3 pymongo 使用 count 报警告解决办法

最近老代码重构,使用的新版本 MongoDB,Python3.7 安装 pymongo 模块,在使用 count 统计数据量的时候报了一个警告:

DeprecationWarning: count is deprecated. Use estimated_document_count or count_documents instead. Please note that $where must be replaced by $expr, $near must be replaced by $geoWithin with $center, and $nearSphere must be replaced by $geoWithin with $centerSphere

大致意思是 count 这个统计函数在新版本 MongoDB 弃用了,改用新的统计函数

estimated_document_count()

将之前的旧版

db_count = cursor.count()

修改为

db_count = cursor.estimated_document_count()

如果是带条件的查询统计就需要使用 count_documents

db_count = cursor.count_documents({'dt': handle_date})

你可能感兴趣的:(#,Python3,基础知识,数据库)