0Day Forums
In MongoDB's pymongo, how do I do a count()? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: Database (https://zeroday.vip/Forum-Database)
+--- Thread: In MongoDB's pymongo, how do I do a count()? (/Thread-In-MongoDB-39-s-pymongo-how-do-I-do-a-count)



In MongoDB's pymongo, how do I do a count()? - Polly550463 - 07-20-2023

for post in db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num):

How do I get the `count()`?


RE: In MongoDB's pymongo, how do I do a count()? - helsabnuaywxav - 07-20-2023

Not sure why you want the count if you are already passing limit 'num'. Anyway if you want to assert, here is what you should do.

results = db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num)

results_count = results.count(True)

That will match **results_count** with **num**


RE: In MongoDB's pymongo, how do I do a count()? - launched2234 - 07-20-2023

If you're using pymongo version 3.7.0 or higher, [see this answer](

[To see links please register here]

) instead.

---

If you want `results_count` to ignore your `limit()`:

results = db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num)
results_count = results.count()

for post in results:

If you want the `results_count` to be capped at your `limit()`, [set `applySkipLimit` to `True`](

[To see links please register here]

):

results = db.datasets.find({"test_set":"abc"}).sort("abc",pymongo.DESCENDING).skip((page-1)*num).limit(num)
results_count = results.count(True)

for post in results:


RE: In MongoDB's pymongo, how do I do a count()? - gadrooned991966 - 07-20-2023

The thing in my case relies in the count of matched elements for a given query, and surely not to repeat this query twice:

**one** to get the count, and
**two** to get the result set.

*no way*

I know the query result set is not quite big and fits in memory, therefore, I can convert it to a list, and get the list length.

This code illustrates the use case:

```python
# pymongo 3.9.0
while not is_over:
it = items.find({"some": "/value/"}).skip(offset).size(limit)
# List will load the cursor content into memory
it = list(it)
if len(it) < size:
is_over = True
offset += size
```


RE: In MongoDB's pymongo, how do I do a count()? - humidify534033 - 07-20-2023

Cannot comment unfortuantely on @Sohaib Farooqi's answer... Quick note: although, ```cursor.count()``` has been deprecated it is significantly faster, than ```collection.count_documents()``` in all of my tests, **when counting all documents in a collection (ie. filter={})**. Running ```db.currentOp()``` reveals that ```collection.count_documents()``` uses an aggregation pipeline, while ```cursor.count()``` doesn't. This might be a cause.


RE: In MongoDB's pymongo, how do I do a count()? - varia331118 - 07-20-2023

Since pymongo version 3.7.0 and above [count() is deprecated](

[To see links please register here]

). Instead use [`Collection.count_documents`](

[To see links please register here]

). Running `cursor.count` or `collection.count` will result in following warning message:

DeprecationWarning: count is deprecated. Use Collection.count_documents instead.

To use `count_documents` the code can be adjusted as follows

import pymongo

db = pymongo.MongoClient()
col = db[DATABASE][COLLECTION]

find = {"test_set":"abc"}
sort = [("abc",pymongo.DESCENDING)]
skip = 10
limit = 10

doc_count = col.count_documents(find, skip=skip)
results = col.find(find).sort(sort).skip(skip).limit(limit)

for doc in result:
//Process Document

**Note:** `count_documents` method performs relatively slow as compared to `count` method. In order to optimize you can use [`collection.estimated_document_count`](

[To see links please register here]

). This method will return estimated number of docs(as the name suggested) based on collection metadata.


RE: In MongoDB's pymongo, how do I do a count()? - anadiplosis63 - 07-20-2023

This thread happens to be 11 years old. However, in 2022 the 'count()' function has been deprecated. Here is a way I came up with to count documents in MongoDB using Python. Here is a picture of the code snippet. Making a empty list is not needed I just wanted to be outlandish. Hope this helps :). [Code snippet here.][1]


[1]:



RE: In MongoDB's pymongo, how do I do a count()? - aprilettehucakouy - 07-20-2023

If you want to use cursor and also want count, you can try this way
```python
# Have 27 items in collection
db = MongoClient(_URI)[DB_NAME][COLLECTION_NAME]

cursor = db.find()
count = db.find().explain().get("executionStats", {}).get("nReturned")
# Output: 27

cursor = db.find().limit(5)
count = db.find().explain().get("executionStats", {}).get("nReturned")
# Output: 5

# Can also use cursor
for item in cursor:
...

```

You can read more about it from

[To see links please register here]




RE: In MongoDB's pymongo, how do I do a count()? - unkinglike835187 - 07-20-2023


if you wants all the records count(without any filter) in a collection then use this:

from pymongo import MongoClient
cl = pymongo.MongoClient(host="localhost", port=27017)
db = cl["database_name"]
print(db.get_collection("collection_name").estimated_document_count())