为了账号安全,请及时绑定邮箱和手机立即绑定

在 python 中使用 pymongo 使用 db.collection.count()

在 python 中使用 pymongo 使用 db.collection.count()

梦里花落0921 2023-06-06 17:34:29
我在用着MongoDB shell version v4.4.0 和 pymongo 3.10.0版本当我使用any_db.any_collection.count()或any_db.any_collection.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  print(f'Total Categories = {db.rank_list_category.count({})}') 我的代码:import pandas as pdfrom src.utils import get_full_pathfrom pymongo import MongoClientfrom bson.objectid import ObjectIdclient = MongoClient('localhost', 27017)db = client['techexpert']print(f'Total Categories = {db["rank_list_category"].count({})}')输出:Total Categories = 5    /home/mobin/PycharmProjects/IMDb/src/database/database_service_provider.py:17: 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  print(f'Total Categories = {db.rank_list_category.count({})}')
查看完整描述

2 回答

?
临摹微笑

TA贡献1982条经验 获得超2个赞

如文档中所述

count() 方法已弃用,在事务中不受支持。请改用 count_documents() 或 estimated_document_count() 。

从 count() 迁移到 count_documents() 时,必须替换以下查询运算符 - $where、$near、$nearSphere

在 3.7 版更改:不推荐使用。

所以使用count_documents


查看完整回答
反对 回复 2023-06-06
?
当年话下

TA贡献1890条经验 获得超9个赞

您收到此警告是因为pymongo弃用了该count函数,这意味着您不应再在新代码中使用它。

改变你的用途:

db.collection.count({})

db.collection.count_documents({})


查看完整回答
反对 回复 2023-06-06
  • 2 回答
  • 0 关注
  • 182 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信