I’m trying to get amount of objects saved in MongoDB with
JavaScript
x
7
1
db = myclient.database_sample
2
my_collection = db["database"]
3
4
5
mydoc = my_collection.find().count()
6
print("The number of documents in collection : ", mydoc)
7
but I’m getting an error
JavaScript
1
3
1
mydoc = my_collection.find().count()
2
AttributeError: 'Cursor' object has no attribute 'count'
3
I’m using Pymongo 2.0
Advertisement
Answer
The find() function for pymongo returns a cursor object (not an array). Pymongo does include a count_documents function. Meaning the code should look like this:
JavaScript
1
2
1
numberOfDocs = my_collection.count_documents({})
2
Edit: Updated to correct solution.