multipolygon
multipolygon

Reputation: 2234

Why can't I get a Mongodb 2.6 text search score using the Ruby driver?

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

setup:

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

This works:

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

This return an error:

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

RuntimeError: Unknown options [{"score"=>{"$meta"=>"textScore"}}]

Upvotes: 2

Views: 309

Answers (1)

Neil Lunn
Neil Lunn

Reputation: 151112

Field selection for projection works a little differently with the ruby driver. You need to specify projection as a list of :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

Upvotes: 3

Related Questions