【问题标题】:Pymongo using hint on find_one gets AttributeErrorPymongo 在 find_one 上使用提示获取 AttributeError
【发布时间】:2023-02-10 03:00:31
【问题描述】:

我需要使用 mongo 执行 mongodb find_one 查询但得到 AttributeError: 'NoneType' object has no attribute 'hint' 因为没有匹配过滤器的结果

db.collection_name.find_one( filter=filter_query, projection={ _id: False, date: True, }, sort=[ ( date, pymongo.DESCENDING, ) ], ).hint('some_index') 也试过

db.collection_name.find_one( filter=filter_query, projection={ _id: False, date: True, }, sort=[ ( date, pymongo.DESCENDING, ) ], hint='some_index' ) 我知道我可以用 find() 做到这一点,但是有没有办法用 find_one 做到这一点?

【问题讨论】:

  • 您显示的第二个find_one 的输出/错误是什么?

标签: pymongo


【解决方案1】:

第一种方法肯定行不通。第二个要求以用于创建索引的相同格式传递提示参数,例如[('field', ASCENDING)]

https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 2018-09-27
    • 1970-01-01
    • 2019-03-31
    • 2021-04-08
    • 1970-01-01
    • 2019-02-02
    • 2021-12-23
    • 2021-05-15
    相关资源
    最近更新 更多