diff --git a/lib/mongo/collection.rb b/lib/mongo/collection.rb index e27ac23..ea5f71f 100644 --- a/lib/mongo/collection.rb +++ b/lib/mongo/collection.rb @@ -670,6 +670,8 @@ module Mongo message = BSON::ByteBuffer.new([0, 0, 0, 0]) BSON::BSON_RUBY.serialize_cstr(message, "#{@db.name}.#{collection_name}") documents.each { |doc| message.put_array(BSON::BSON_CODER.serialize(doc, check_keys, true).to_a) } + raise InvalidOperation, "Exceded maximum insert size of 16,000,000 bytes" if message.size > 16_000_000 + if safe @connection.send_message_with_safe_check(Mongo::Constants::OP_INSERT, message, @db.name, "#{@db.name}['#{collection_name}'].insert(#{documents.inspect})", safe) diff --git a/test/collection_test.rb b/test/collection_test.rb index 02542c9..6bc90f0 100644 --- a/test/collection_test.rb +++ b/test/collection_test.rb @@ -115,6 +115,17 @@ class TestCollection < Test::Unit::TestCase end end + def test_maximum_insert_size + docs = [] + 16.times do + docs << {'foo' => 'a' * 1_000_000} + end + + assert_raise InvalidOperation do + @@test.insert(docs) + end + end + if @@version >= "1.5.1" def test_safe_mode_with_advanced_safe_with_invalid_options assert_raise_error ArgumentError, "Unknown key(s): wtime" do