diff --git a/README b/README index 57de8b1..c1e095c 100644 --- a/README +++ b/README @@ -52,6 +52,13 @@ type = To Do +* Implement Babble's algorithm for ObjectID. Implement Comparable. + +* Change find(selector={}, fields=nil, options={}) to find(selector={}, + options={}) + +* ObjectID equality. + * Capped collection support. * More code comments. More text in this file. diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 135c7fa..91181f5 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -33,7 +33,8 @@ module XGen def insert(*objects) objects = objects.first if objects.size == 1 && objects.first.is_a?(Array) - @db.insert_into_db(@name, objects) + res = @db.insert_into_db(@name, objects) + res.size > 1 ? res : res.first end alias_method :<<, :insert diff --git a/lib/mongo/objectid.rb b/lib/mongo/objectid.rb index a386ee9..596cf81 100644 --- a/lib/mongo/objectid.rb +++ b/lib/mongo/objectid.rb @@ -20,6 +20,8 @@ module XGen class ObjectID + include Comparable + @@uuid_generator = UUID.new # String UUID @@ -35,6 +37,12 @@ module XGen def to_s @uuid end + + # This will make more sense when we implement the Babble algorithm for + # generating object ids. + def <=>(other) + to_s <=> other.to_s + end end end end diff --git a/tests/test_db_api.rb b/tests/test_db_api.rb index 1566d93..c23894c 100644 --- a/tests/test_db_api.rb +++ b/tests/test_db_api.rb @@ -6,7 +6,9 @@ require 'test/unit' class DBAPITest < Test::Unit::TestCase def setup - @db = XGen::Mongo::Driver::Mongo.new.db('ruby-mongo-test') + host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' + port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + @db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-test') @coll = @db.collection('test') @coll.clear @coll.insert('a' => 1) # collection not created until it's used @@ -109,4 +111,41 @@ class DBAPITest < Test::Unit::TestCase assert_equal 1, rows.length assert_equal regex, rows[0]['b'] end + + def test_find + @coll.insert('a' => 2) + + # Find by advanced query (less than) + docs = @coll.find('a' => { '$lt' => 10 }).map + assert_equal 2, docs.size + assert docs.detect { |row| row['a'] == 1 } + assert docs.detect { |row| row['a'] == 2 } + + # Find by advanced query (greater than) + docs = @coll.find('a' => { '$gt' => 1 }).map + assert_equal 1, docs.size + assert docs.detect { |row| row['a'] == 2 } + + # Find by advanced query (less than or equal to) + docs = @coll.find('a' => { '$lte' => 1 }).map + assert_equal 1, docs.size + assert docs.detect { |row| row['a'] == 1 } + + # Find by advanced query (greater than or equal to) + docs = @coll.find('a' => { '$gte' => 1 }).map + assert_equal 2, docs.size + assert docs.detect { |row| row['a'] == 1 } + assert docs.detect { |row| row['a'] == 2 } + + # Find by advanced query (between) + docs = @coll.find('a' => { '$gt' => 1, '$lt' => 3 }).map + assert_equal 1, docs.size + assert docs.detect { |row| row['a'] == 2 } + + # Find by advanced query (in clause) + docs = @coll.find('a' => {'$in' => [1,2]}).map + assert_equal 2, docs.size + assert docs.detect { |row| row['a'] == 1 } + assert docs.detect { |row| row['a'] == 2 } + end end diff --git a/tests/test_db_connection.rb b/tests/test_db_connection.rb index 53a48ac..ea3e419 100644 --- a/tests/test_db_connection.rb +++ b/tests/test_db_connection.rb @@ -6,7 +6,9 @@ require 'test/unit' class DBConnectionTest < Test::Unit::TestCase def test_no_exceptions - db = XGen::Mongo::Driver::Mongo.new.db('ruby-mongo-demo') + host = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost' + port = ENV['MONGO_RUBY_DRIVER_PORT'] || XGen::Mongo::Driver::Mongo::DEFAULT_PORT + db = XGen::Mongo::Driver::Mongo.new(host, port).db('ruby-mongo-demo') coll = db.collection('test') coll.clear end