在 python 中使用 pymongo 使用 db.collection.count() 函数时出现 DeprecationWarning?
getting DeprecationWarning while using db.collection.count() function using pymongo in python?
我正在使用
MongoDB shell version v4.4.0
and
pymongo 3.10.0
version
当我使用 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({})}')
您收到此警告是因为 pymongo
弃用了 count
函数,这意味着您不应再在新代码中使用它。
更改您的用途:
db.collection.count({})
到
db.collection.count_documents({})
The count() method is deprecated and not supported in a transaction. Please use count_documents() or estimated_document_count() instead.
When migrating from count() to count_documents() the following query operators must be replaced - $where, $near, $nearSphere
Changed in version 3.7: Deprecated.
所以使用count_documents
我正在使用
MongoDB shell version v4.4.0
andpymongo 3.10.0
version
当我使用 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({})}')
您收到此警告是因为 pymongo
弃用了 count
函数,这意味着您不应再在新代码中使用它。
更改您的用途:
db.collection.count({})
到
db.collection.count_documents({})
The count() method is deprecated and not supported in a transaction. Please use count_documents() or estimated_document_count() instead.
When migrating from count() to count_documents() the following query operators must be replaced - $where, $near, $nearSphere
Changed in version 3.7: Deprecated.
所以使用count_documents