【问题标题】:Using $natural sort in pymongo - MongoDB [duplicate]在 pymongo 中使用 $natural 排序 - MongoDB [重复]
【发布时间】:2019-03-25 01:56:04
【问题描述】:

我知道在 MongoDB 中使用 $natural 排序可以提高磁盘吞吐量,因为光标将读取顺序记录而不是四处跳转。

但是,当我尝试使用此 SO example 或遵循 Cursor.sort() Ref Docs 时,我收到以下错误:

for t in collection.find({'raw.lang': 'en'}).sort( { $natural: 1 } ):

不出所料地给出了语法错误。

for t in collection.find({'raw.lang': 'en'}).sort( { '$natural': 1 } ):

发生异常:TypeError 如果没有指定方向,key_or_list 必须是 list 的一个实例

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: python mongodb pymongo


    【解决方案1】:

    传递异常中提到的排序键数组,

    for t in collection.find({'raw.lang': 'en'}).sort([( '$natural', 1 )] ):
    

    【讨论】:

    • 使用的实际工作代码是: for t in collection.find({'raw.lang': 'en'}).sort([( '$natural', 1 )] ): 注意逗号而不是冒号。
    • 是的,我的错。更新了答案。
    • 是的,$natural 周围的逗号和方括号在 pymongo 中与常规 MongoDB 查询不同。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-07-15
    • 2017-09-08
    • 1970-01-01
    • 2014-05-19
    • 1970-01-01
    • 2017-08-30
    • 2018-05-17
    • 2020-10-20
    相关资源
    最近更新 更多