Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning message in PyMongo: count is deprecated

Tags:

mongodb

def post(self):
    if db.users.find({"email": email}).count() != 0:
        abort(400, message="email is alread used.")

DeprecationWarning: count is deprecated. Use Collection.count_documents instead.

I'm making authentication server with Python-Flask and PyMongo package. Every time post() method is called, above deprecation warning message is displayed.

def post(self):
    if db.users.find({"email": email}).count_documents() != 0:
        abort(400, message="email is alread used.")

However, if I change count() to count_documents(), following error message comes out.

AttributeError: 'Cursor' object has no attribute 'count_documents'

How do I call count_documents() correctly after find() is called?

like image 348
EnDelt64 Avatar asked May 25 '19 09:05

EnDelt64


1 Answers

The method count_documents is part of the collection, not the cursor (find returns a cursor). Please see the PyMongo documentation regarding the method for more information and a note regarding some operators.

def post(self):
    if db.users.count_documents({"email": email}) != 0:
        abort(400, message="email is alread used.")
like image 191
Plancke Avatar answered Nov 01 '22 12:11

Plancke