Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 429
maniac_user Avatar asked Apr 24 '13 23:04

maniac_user


3 Answers

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()
like image 95
cacahootie Avatar answered Sep 17 '22 11:09

cacahootie


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.

like image 25
Wenuka Avatar answered Sep 20 '22 11:09

Wenuka


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.

like image 27
Xavier Guihot Avatar answered Sep 19 '22 11:09

Xavier Guihot