PyMongo 在全文搜索中选择字段

1 投票
1 回答
3913 浏览
提问于 2025-04-18 17:43

大家好,
我在尝试使用 find 方法中的 fields 参数时,遇到了以下错误代码:

TypeError: __init__() got multiple values for keyword argument 'fields'

代码:

mongo.db.products.find({ '$text': { '$search': string } }, { 'score': { '$meta': 'textScore' } }, fields=('name', 'foo', 'bar',))

在没有全文搜索的情况下,fields 参数运行得很好。

1 个回答

4

对于pymongo来说,'textScore'的“投影部分”需要在完整的“字段”说明中包含:

mongo.db.products.find(
    { '$text': { '$search': string } },
    fields=({ 'name': 1, 'foo': 1, 'bar': 1, 'score': { '$meta': 'textScore' } )
)

撰写回答