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)
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()
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With