gpt4 book ai didi

python - TypeError : if no direction is specified, key_or_list 必须是 list 的一个实例

转载 作者:行者123 更新时间:2023-12-04 11:43:00 26 4
gpt4 key购买 nike

我在行 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

最佳答案

仅尝试:

collection.find().sort("_id", -1)

关于python - TypeError : if no direction is specified, key_or_list 必须是 list 的一个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57778658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com