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

我在用着


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 pd

from src.utils import get_full_path

from pymongo import MongoClient

from bson.objectid import ObjectId


client = 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({})}')


梦里花落0921
浏览 279回答 2
2回答

临摹微笑

如文档中所述count() 方法已弃用,在事务中不受支持。请改用 count_documents() 或 estimated_document_count() 。从 count() 迁移到 count_documents() 时,必须替换以下查询运算符 - $where、$near、$nearSphere在 3.7 版更改:不推荐使用。所以使用count_documents

当年话下

您收到此警告是因为pymongo弃用了该count函数,这意味着您不应再在新代码中使用它。改变你的用途:db.collection.count({})到db.collection.count_documents({})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python