Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 239 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
In MongoDB's pymongo, how do I do a count()?

#1
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()`?
Reply

#2
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**
Reply

#3
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:
Reply

#4
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
```
Reply

#5
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.
Reply

#6
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.
Reply

#7
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]:
Reply

#8
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]

Reply

#9

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())
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through