diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 75cb69c..967d868 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -299,10 +299,10 @@ module Mongo selector when nil {} - when String - {"$where" => Code.new(selector)} - when Code + when BSON::Code {"$where" => selector} + when String + {"$where" => BSON::Code.new(selector)} end end diff --git a/test/db_api_test.rb b/test/db_api_test.rb index 5a5113b..9bc88f5 100644 --- a/test/db_api_test.rb +++ b/test/db_api_test.rb @@ -469,8 +469,18 @@ class DBAPITest < Test::Unit::TestCase @@coll.insert('a' => 3) assert_equal 3, @@coll.count - assert_equal 1, @@coll.find('$where' => Code.new('this.a > 2')).count() - assert_equal 2, @@coll.find('$where' => Code.new('this.a > i', {'i' => 1})).count() + assert_equal 1, @@coll.find('$where' => BSON::Code.new('this.a > 2')).count() + assert_equal 2, @@coll.find('$where' => BSON::Code.new('this.a > i', {'i' => 1})).count() + end + + def test_implicit_where + @@coll.remove + @@coll.insert('a' => 2) + @@coll.insert('a' => 3) + + assert_equal 2, @@coll.count + assert_equal 1, @@coll.find('this.a > 2').count() + assert_equal 2, @@coll.find(BSON::Code.new('this.a > z', {'z' => 1})).to_a.length end def test_eval diff --git a/test/mongo_bson/bson_test.rb b/test/mongo_bson/bson_test.rb index 0290802..53f664a 100644 --- a/test/mongo_bson/bson_test.rb +++ b/test/mongo_bson/bson_test.rb @@ -98,6 +98,12 @@ class BSONTest < Test::Unit::TestCase assert_equal doc, BSON::BSON_CODER.deserialize(bson) end + def test_code_with_scope + doc = {'$where' => Code.new('this.a.b < this.b', {'foo' => 1})} + bson = BSON::BSON_CODER.serialize(doc) + assert_equal doc, BSON::BSON_CODER.deserialize(bson) + end + def test_number doc = {'doc' => 41.99} bson = BSON::BSON_CODER.serialize(doc)