How to Iterate a mongo cursor in reverse in python?

时间:2019-01-09 21:59:38

标签: database mongodb nosql

How do you iterate through a pymongo cursor object in reverse? I tried using the reversed built-in but there was an error:

TypeError: object of type 'Cursor' has no len()

My code:

for document in reversed(cursor):
    documents.append(document)

1 个答案:

答案 0 :(得分:0)

Looks like cursor can't be reserved but I can specify in sort a negative value to return a cursor with values stored in reverse:

collection.find(query, {}).sort('some_field_to_sort_by' : -1)