簡體   English   中英

使用 db.collection.count() function 在 python 中使用 pymongo 時得到 DeprecationWarning?

[英]getting DeprecationWarning while using db.collection.count() function using pymongo in python?

我在用着

MongoDB shell version v4.4.0pymongo 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({})}')

Output:

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

文檔中所述

count() 方法已被棄用,並且在事務中不受支持。 請改用 count_documents() 或estimated_document_count()。

從 count() 遷移到 count_documents() 時,必須替換以下查詢運算符 - $where、$near、$nearSphere

在 3.7 版更改:已棄用。

所以使用count_documents

您收到此警告是因為pymongo正在棄用count function,這意味着您不應再在新代碼中使用它。

更改您的用途:

db.collection.count({})

db.collection.count_documents({})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM