TypeError: object of type 'Cursor' has no len()


Question

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)
1
15
10/26/2018 8:55:23 AM

Accepted Answer

EDIT:

Just noticed you referenced mongodb in your tags. I was confused because the body of your question didn't specify which database you are using.

The cursor has a method 'count()' which will return what you're looking for.

PyMongo Cursor Docs

22
4/24/2013 11:52:53 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon