python使用mongodb进行排序时的错误

  • A+
所属分类:程序开发

使用pymongo对某一字段进行sort时出现了致命错误

报错内容

$ pipenv run python spiders/weibo.py 
Traceback (most recent call last):
  File "spiders/weibo.py", line 241, in <module>
    res = MongoModel().table('photo').where({"laiyuan":"weibo"}).select({"pic_name":True,"timestamp":True,"_id":False})
  File ".\database\mongoModel.py", line 56, in select
    res = self.con.find(self._where, _field).sort({"timestamp":1})
  File "C:\Users\guo5z\.virtualenvs\photo_view-LolT592W\lib\site-packages\pymongo\cursor.py", line 726, in sort
    keys = helpers._index_list(key_or_list, direction)
  File "C:\Users\guo5z\.virtualenvs\photo_view-LolT592W\lib\site-packages\pymongo\helpers.py", line 73, 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

问题代码

self.con.find(self._where, _field).sort({"timestamp":-1})

修改代码

self.con.find(self._where, _field).sort([("timestamp",-1)])
  • 公众号
  • 扫一扫
  • weinxin
  • 打赏
  • 扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: