【问题标题】:pymongo key_or_list must be an instance of listpymongo key_or_list 必须是 list 的一个实例
【发布时间】:2021-08-09 17:54:32
【问题描述】:

有人可以帮助我并告诉我如何解决这个 pymongo 错误吗?

  cluster = pymongo.MongoClient("xxx")
  db = cluster['Coins']
  ada_db = db['ada']
  eos_db = db['eos']
  miota_db = db['miota']
  nano_db = db['nano']
  xrp_db = db['xrp']
  xmr_db = db["xmr"]

  BTC = getPrice("BTC")
  ETH = getPrice("ETH")
  XRP = getPrice("XRP")
  EOS = getPrice("EOS")
  ADA = getPrice("ADA")
  IOTA =getPrice("IOTA")
  NANO =getPrice("NANO")
  XMR = getPrice("XMR")

  ada_db.insertOne({'_id': (ada_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': ADA})
  eos_db.insertOne({'_id': (eos_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': EOS})
  miota_db.insertOne({'_id': (miota_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': IOTA})
  nano_db.insertOne({'_id': (nano_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': NANO})
  xrp_db.insertOne({'_id': (xrp_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': XRP})
  xmr_db.insertOne({'_id': (xmr_db.find({}).sort({"_id": -1}).limit(1) + 1), 'value': XMR})
TypeError: if no direction is specified, key_or_list must be an instance of list

如果你能帮助我会很酷:微笑:

ada_db.insertOne({'_id': (ada_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': ADA})

这是 pycharm 标记错误的地方

    ada_db.insertOne({'_id': (ada_db.find({}).sort({"_id":-1}).limit(1) + 1), 'value': ADA})
  File "C:\Users\brend\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\cursor.py", line 762, in sort
    keys = helpers._index_list(key_or_list, direction)
  File "C:\Users\brend\AppData\Local\Programs\Python\Python39\lib\site-packages\pymongo\helpers.py", line 74, 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```

【问题讨论】:

标签: python mongodb pymongo


【解决方案1】:

取自TypeError: if no direction is specified, key_or_list must be an instance of list中的答案

删除.find() 中的{} 并使用2 个参数而不是.sort() 中的字典

ada_db.insertOne({'_id': (ada_db.find().sort("_id", -1).limit(1) + 1), 'value': ADA})

【讨论】:

    猜你喜欢
    • 2020-01-06
    • 2021-03-03
    • 2018-03-12
    • 2021-06-20
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多