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

后端 未结 6 1459
南笙
南笙 2021-02-19 01:49

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

6条回答
  •  独厮守ぢ
    2021-02-19 02:35

    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)
    

提交回复
热议问题