在collection.find({}).sort({"_id":-1})
行出现错误,如何在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)
错误:-
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
答案 0 :(得分:0)
仅尝试使用“ collection.find()。sort(” _ id“,-1)”。
答案 1 :(得分:0)
collection.find().sort("_id":-1)
应该这样做-不带'{}'。
您也可以尝试:
collection.find().sort("_id": pymongo.ASCENDING)
在使用python