AttributeError:‘Cursor’ object has no attribute ‘count’
在统计查询结果包含多少条数据的时候,一开始使用的是find().count()进行统计,代码如下:
import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students
count = collection.find().count()
print(count)
运行结果如下:
在网上查找资料发现,count方法已经被新版本淘汰了,我们可以使用新的统计方法estimated_document_count()
db_count = cursor.estimated_document_count()
如果是带条件的查询统计就需要使用count_documents
db_count = cursor.count_documents({'dt': handle_date})
后面我将count()修改为estimated_document_count(),运行之后,发现还是报错
后来找到了原因,发现把find()方法去掉就可以了
import pymongo
client = pymongo.MongoClient(host='localhost', port=27017)
db = client.test
collection = db.students
count = collection.estimated_document_count()
print(count)
运行结果如下:
这样问题就解决了。。。