gpt4 book ai didi

ruby - 为什么我不能使用 Ruby 驱动程序获得 Mongodb 2.6 文本搜索分数?

转载 作者:可可西里 更新时间:2023-11-01 10:04:14 25 4
gpt4 key购买 nike

mongoDB 2.6.2 版

gem 'mongo', '~> 1.10.2'

http://docs.mongodb.org/manual/reference/operator/query/text/#return-the-text-search-score

https://github.com/mongodb/mongo-ruby-driver/wiki/Tutorial

设置:

require 'mongo'
mongo_client = Mongo::MongoClient.new("localhost", 27017)
db = mongo_client.db("mydb")
coll = db.collection("testCollection")

这个有效:

coll.find({ "$text" => { "$search" => "one two three" } }).to_a

这会返回一个错误:

coll.find({ "$text" => { "$search" => "one two three" } }, { "score" => { "$meta" => "textScore" }}).to_a

运行时错误:未知选项 [{"score"=>{"$meta"=>"textScore"}}]

最佳答案

投影的字段选择与 ruby​​ 驱动程序的工作方式略有不同。您需要将投影指定为 :fields:

的列表
require 'mongo'
include Mongo

client = MongoClient.new
db = client['test']
coll = db['text']

puts coll.find(
{ "$text" => { "$search" => "one two three" } },
{ :fields => [{ "score" => { "$meta" => "textScore" } }] }
).to_a

关于ruby - 为什么我不能使用 Ruby 驱动程序获得 Mongodb 2.6 文本搜索分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24623095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com