diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index 6e6a86e33c..e575f864e8 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -729,7 +729,8 @@ def aggregate(pipeline=nil, opts={}) seed = { :cursor_id => cursor_info['id'], :first_batch => cursor_info['firstBatch'], - :pool => pinned_pool + :pool => pinned_pool, + :ns => cursor_info['ns'] } return Cursor.new(self, seed.merge!(opts)) @@ -901,7 +902,8 @@ def parallel_scan(num_cursors, opts={}) seed = { :cursor_id => cursor_info['cursor']['id'], :first_batch => cursor_info['cursor']['firstBatch'], - :pool => pinned_pool + :pool => pinned_pool, + :ns => cursor_info['ns'] } Cursor.new(self, seed.merge!(opts)) end diff --git a/lib/mongo/cursor.rb b/lib/mongo/cursor.rb index 983d5837ae..5837729ade 100644 --- a/lib/mongo/cursor.rb +++ b/lib/mongo/cursor.rb @@ -23,8 +23,7 @@ class Cursor include Mongo::ReadPreference attr_reader :collection, :selector, :fields, - :order, :hint, :snapshot, :timeout, - :full_collection_name, :transformer, + :order, :hint, :snapshot, :timeout, :transformer, :options, :cursor_id, :show_disk_loc, :comment, :compile_regex, :read, :tag_sets, :acceptable_latency @@ -40,6 +39,7 @@ def initialize(collection, opts={}) @cursor_id = opts.delete(:cursor_id) @db = collection.db @collection = collection + @ns = opts.delete(:ns) @connection = @db.connection @logger = @connection.logger @@ -83,7 +83,6 @@ def initialize(collection, opts={}) batch_size(opts.delete(:batch_size) || 0) - @full_collection_name = "#{@collection.db.name}.#{@collection.name}" @cache = opts.delete(:first_batch) || [] @returned = 0 @@ -124,6 +123,10 @@ def alive? @cursor_id && @cursor_id != 0 end + def full_collection_name + @ns || "#{@collection.db.name}.#{@collection.name}" + end + # Get the next document specified the cursor options. # # @return [Hash, Nil] the next document or Nil if no documents remain. @@ -484,7 +487,7 @@ def query_options_hash # Clean output for inspect. def inspect - "" end @@ -580,7 +583,7 @@ def send_get_more message = BSON::ByteBuffer.new([0, 0, 0, 0]) # DB name. - BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}") + BSON::BSON_RUBY.serialize_cstr(message, full_collection_name) # Number of results to return. if @limit > 0 @@ -636,7 +639,7 @@ def checkin_socket(sock) def construct_query_message message = BSON::ByteBuffer.new("", @connection.max_bson_size + MongoClient::COMMAND_HEADROOM) message.put_int(@options) - BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{@collection.name}") + BSON::BSON_RUBY.serialize_cstr(message, full_collection_name) message.put_int(@skip) @batch_size > 1 ? message.put_int(@batch_size) : message.put_int(@limit) if query_contains_special_fields? && @bson # costs two serialize calls diff --git a/test/unit/cursor_test.rb b/test/unit/cursor_test.rb index 749198ab5f..a8c9ab04ae 100644 --- a/test/unit/cursor_test.rb +++ b/test/unit/cursor_test.rb @@ -138,7 +138,15 @@ class Mongo::Cursor assert_equal 100, @cursor.batch_size end - context "conected to mongos" do + context 'when an alternate namespace is specified' do + + should 'use the alternate namespace' do + cursor = Cursor.new(@collection, :ns => 'other_db.other_collection') + assert_equal('other_db.other_collection', cursor.full_collection_name) + end + end + + context "connected to mongos" do setup do @connection.stubs(:mongos?).returns(true) @tag_sets = [{:dc => "ny"}] @@ -211,7 +219,7 @@ class Mongo::Cursor end end - context "not conected to mongos" do + context "not connected to mongos" do setup do @connection.stubs(:mongos?).returns(false) end