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

时间:2014-07-08 03:10:54

标签: ruby mongodb

mongoDB v 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

RuntimeError:未知选项[{“得分”=> {“$ meta”=>“textScore”}}]

1 个答案:

答案 0 :(得分:3)

投影的字段选择与红宝石驱动程序的工作方式略有不同。您需要将投影指定为: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