PyMongo排序与元数据

斯卡兹

我想知道如何将跟随mongodb查询转换为pymongo语法

db.articles.find(
   { $text: { $search: "cake" } },
   { score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } ).limit(3)

我尝试了这个:

results = \
    mongo.db.products.find({ '$text': { '$search': 'cake' } }, { 'score': { '$meta': 'textScore' } }) \
        .sort({ 'score': { '$meta': 'textScore' } }) \
        .limit(3)

但是我得到以下错误:

raise TypeError("second item in each key pair must be 1, -1, "
TypeError: second item in each key pair must be 1, -1, '2d', 'geoHaystack', or another valid MongoDB index specifier.

有人可以帮助我吗?提前致谢

Horacioibrahim

我认为解决方案在这里:https : //github.com/mongodb/mongo-python-driver/blob/master/pymongo/cursor.py#L658要添加具有“新”功能“ $ text”的新方法的(键,方向)列表:

    Beginning with MongoDB version 2.6, text search results can be
    sorted by relevance::

        cursor = db.test.find(
            {'$text': {'$search': 'some words'}},
            {'score': {'$meta': 'textScore'}})

        # Sort by 'score' field.
        cursor.sort([('score', {'$meta': 'textScore'})]) #<<<< HERE

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章