Mongodb PyMongo中的警告消息:计数已弃用

Mongodb PyMongo中的警告消息:计数已弃用,mongodb,Mongodb,弃用警告:计数已弃用。改用Collection.count\u文档。 我正在用PythonFlask和PyMongo包制作身份验证服务器。每次调用post()方法时,都会显示上述弃用警告消息 def post(self): if db.users.find({"email": email}).count() != 0: abort(400, message="email is alread used.") 但是,如果我将count()更改为count\u documen

弃用警告:计数已弃用。改用Collection.count\u文档。

我正在用PythonFlask和PyMongo包制作身份验证服务器。每次调用
post()
方法时,都会显示上述弃用警告消息

def post(self):
    if db.users.find({"email": email}).count() != 0:
        abort(400, message="email is alread used.")
但是,如果我将
count()
更改为
count\u documents()
,则会出现以下错误消息

AttributeError:“Cursor”对象没有属性“count\u documents”


调用
find()
后如何正确调用
count\u documents()

方法
count\u documents
集合的一部分,而不是
光标
find
返回光标)。 有关某些操作员的更多信息和说明,请参阅

def post(self):
    if db.users.find({"email": email}).count_documents() != 0:
        abort(400, message="email is alread used.")
报告说: “数据库”对象没有“count\u documents”属性


如何修复它?

那么,当不必要地使用count on cursor时,为什么它会发出警告?它是否会检索数据两次(即在数据库上使用大量i/O),一次获取光标的数据,一次获取计数的数据?请注意,count_文档需要
{}
至少第一个参数
count\u documents
是集合的属性,而不是数据库。请尝试-
db.ut.count\u文档({})
来修复此问题。
def post(self):
    if db.users.count_documents({"email": email}) != 0:
        abort(400, message="email is alread used.")
change  : print(db.find("ut", {}).count())
to : print(db.count_documents("ut", {}))