Skh*_*haz 2 python mongodb pymongo mongodb-query
我想知道如何将跟随mongodb查询转换为pymongo语法
db.articles.find(
{ $text: { $search: "cake" } },
{ score: { $meta: "textScore" } }
).sort( { score: { $meta: "textScore" } } ).limit(3)
Run Code Online (Sandbox Code Playgroud)
我试过这个:
results = \
mongo.db.products.find({ '$text': { '$search': 'cake' } }, { 'score': { '$meta': 'textScore' } }) \
.sort({ 'score': { '$meta': 'textScore' } }) \
.limit(3)
Run Code Online (Sandbox Code Playgroud)
但是我在排序时遇到了以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?提前致谢
小智 5
我认为解决方案在这里: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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1545 次 |
| 最近记录: |