我在用著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

當年話下
TA貢獻1890條經驗 獲得超9個贊
您收到此警告是因為pymongo
棄用了該count
函數,這意味著您不應再在新代碼中使用它。
改變你的用途:
db.collection.count({})
到
db.collection.count_documents({})
添加回答
舉報
0/150
提交
取消