I am running into error at line collection.find({}).sort({"_id":-1})
,how do I sort a collection of documents in mongodb?
count = 1
cursor = collection.find({}).sort({"_id":-1})
for document in cursor:
if count !=100:
logger.info('COUNT %s'%count)
logger.info('document %s'%document)
else:
logger.info('Exiting...')
sys.exit(0)
Error:-
Traceback (most recent call last):
File "ibait_data_integrity.py", line 79, in <module>
main()
File "ibait_data_integrity.py", line 66, in main
cursor = collection.find({}).sort({"_id":-1})
File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 703, in sort
keys = helpers._index_list(key_or_list, direction)
File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 52, in _index_list
raise TypeError("if no direction is specified, "
TypeError: if no direction is specified, key_or_list must be an instance of list
try with just:
collection.find().sort("_id", -1)
collection.find().sort("_id",-1)
should do it - without the '{}'.
you can also try :
collection.find().sort("_id", pymongo.ASCENDING)
when using python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With