TypeError: object of type 'Cursor' has no len()
25,358
Solution 1
The pymongo cursor has a method count()
which will return what you're looking for:
reply = db['test'].find(
{"date":{"$gt":date_query}},
{"date":1,"route_id":1,"loc":1,"_id":0}
)
length = reply.count()
Solution 2
Yes, count will do the work for you.
length = reply.count()
or
length = reply.count(with_limit_and_skip=False)
had to suffer a lot coz length = count(reply) also did not work. Since I'm not allowed to comment yet, thought to leave this answer. Hope this will help somebody to save some time.
Solution 3
Starting Mongo 4.0.3
/PyMongo 3.7.0
, you could alternatively use count_documents
instead of count
on a cursor
:
db.collection.count_documents({ "a": 2 })
# where { "a": 2 } is whatever filtering query
db.collection.count_documents
is the alternative to the now deprecated db.collection.count
.
Author by
maniac_user
Updated on July 09, 2022Comments
-
maniac_user almost 2 years
I get this error:
TypeError: object of type 'Cursor' has no len()
when I try to execute:
reply = db['test'].find({"date":{"$gt":date_query}} ,{"date":1,"route_id":1,"loc":1,"_id":0}) length = len(reply)