How to get the length of a cursor from mongodb using python?
Asked Answered
H

11

49

I'm looking for a feasible way to get the length of cursor got from MongoDB.

Heart answered 29/2, 2016 at 5:44 Comment(2)
cursor.count()?Shoifet
Deprecated in 3.7Montiel
A
47

It's actually as simple as

len(list(cursor))

Note that it will however consume the cursor.

Aseptic answered 15/1, 2020 at 16:4 Comment(3)
as of Sept 2020, cursor.count() returns a '''DeprecationWarning: count is deprecated. Use Collection.count_documents instead.''' While count_document returns: '''AttributeError: 'Cursor' object has no attribute 'count_documents'''' So len(list(cursor)) is what works better.Giza
When you say consume, do you mean move the position of the cursor, or does not convert the whole cursor contents (which could be huge) into a list?Discomfiture
@Discomfiture I mean that the cursor is empty afterwards. I’d assume the content is converted into a list, which indeed may not be suitable for all usecases, but I didn’t test, maybe there are some optimizations indeed.Aseptic
E
24

The cursor.count method is deprecated since pymongo 3.7.

The recommended method is to use the count_documents method of the collection.

Eremite answered 11/1, 2019 at 13:32 Comment(6)
does this mean if you want the count before you iterate you have to query twice - once for the collection.count_documents() and another for collection.find()?Stroh
Yes. One needs to call count_documents in an independent query. cursor.count also emitted an independent query, so this is not really a regression. However, this can be an issue for functions that take a cursor as input and need the cursor length. Those functions must be modified to also take the total as parameter, and caller code must be modified accordingly. (It might be possible to access collection and query filters from the cursor, as those are stored as double-underscored attributes, but that sounds bad.)Albuquerque
@Jérôme, but how do you propose to get the collection from the cursor resulting from a find?Bloodthirsty
@Bloodthirsty cursor.collection?Albuquerque
Sorry, I didn't understand that I can apply the filter condition with count_documents(). Now it makes sense.Bloodthirsty
*** AttributeError: 'Cursor' object has no attribute 'count'Kakalina
S
24

The variant of the previous answer:

len(list(cursor.clone()))

doesn't consume cursor

Sanguinary answered 26/3, 2021 at 8:27 Comment(1)
When you say consume, do you mean move the position of the cursor, or does not convert the whole cursor contents (which could be huge) into a list?Discomfiture
A
7

cursor.count()

Counts the number of documents referenced by a cursor. Append the count() method to a find() query to return the number of matching documents. The operation does not perform the query but instead counts the results that would be returned by the query.

db.collection.find(<query>).count()

https://docs.mongodb.com/manual/reference/method/db.collection.count/

Amblyopia answered 29/2, 2016 at 6:13 Comment(2)
cursor.count() is deprecated since pymongo 3.7. See my answer.Albuquerque
cursor.count() is deprecated and the new countDocuments is more accurate but much slower seeOrigan
B
7

len(list(cursor.clone())) worked really well for me, does not consume the editor so it can be use straight with your variable

Bialystok answered 2/2, 2022 at 14:33 Comment(0)
D
3

According to the pymongo documentation, a Pymongo cursor, has a count method:

count(with_limit_and_skip=False)

By default this method returns the total length of the cursor, for example:

cursor.count()

If you call this method with with_limit_and_skip=True, the returned value takes limit and skip queries into account. For example, the following query will return 5 (assuming you have more than 5 documents):

cursor.limit(5).count(True)
Delvecchio answered 29/2, 2016 at 6:24 Comment(1)
cursor.count() is deprecated since pymongo 3.7. See my answer.Albuquerque
H
3

I find that using cursor.iter().count() is a feasible way to resolve this problem

Heart answered 29/2, 2016 at 7:21 Comment(1)
Traceback (most recent call last): File "test.py", line 12, in <module> print (cursor.iter().count()) AttributeError: 'Cursor' object has no attribute 'iter'Stroh
C
1

For some reason, some aggregations return an object that doesn't have the same methods, maybe different class, simple solution, convert the pseudo cursor to an array:

// A simple aggregation with `group`
var cursor = db.getCollection('collection').aggregate([
    {$match: {
        "property": {"$exists": true }
    }},
    {$group: { 
        _id: '$groupable',
        count: {$sum: 1}
    }},
    {$sort: {
        count: -1
    }}
]);

// Converting the "cursor" into an array
var cursor_array = cursor.toArray();

// Looping as an array using `for` instead of `while`
for (var i = 0; i < cursor_array.length; i++) {
    print(cursor_array[i]._id+'\t'+cursor_array[i].count);
}

Notice this solution is only for the shell, I don't know if this array method exists in other libraries.

Crayton answered 4/3, 2020 at 21:27 Comment(1)
the OP asked for Python, you posted something closer to javascriptRailing
P
1

I was able to do count it this way:

def count():
    collection = db[col_name]
    count = collection.count_documents({"visited" : True})
    
    return count
Premiere answered 7/12, 2021 at 18:42 Comment(0)
T
0

How about sum(1 for _ in cursor.clone()) so that you get the count but using constant memory instead of creating a new list. And you don't have to make another query to mongo with this solution.

Temperate answered 20/2, 2023 at 17:14 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Spermophile
S
0

Hello I was trying to find a solution to this and I figured it out.

#Count documents takes a filter object inside(a dict) and you need to call it
#after a Collection object not cursor.
portfolio.count_documents({})

I hope this helps

Sherrellsherrer answered 8/7, 2023 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.