File tree Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Expand file tree Collapse file tree 2 files changed +32
-5
lines changed Original file line number Diff line number Diff line change @@ -75,12 +75,12 @@ def find_one(selector = nil)
75
75
# @since 2.0.0
76
76
def insert_one ( file )
77
77
files_collection . insert_one ( file . metadata )
78
- result = chunks_collection . insert_many ( file . chunks )
79
- if write_concern . get_last_error
80
- validate_md5! ( file )
81
- else
82
- result
78
+ inserts = file . chunks . reduce ( [ ] ) do |ops , chunk |
79
+ ops << { :insert_one => chunk }
83
80
end
81
+ result = chunks_collection . bulk_write ( inserts , ordered : true )
82
+ validate_md5! ( file ) if write_concern . get_last_error
83
+ result
84
84
end
85
85
86
86
# Create the GridFS.
Original file line number Diff line number Diff line change 101
101
} . to raise_error ( Mongo ::Error ::OperationFailure )
102
102
end
103
103
end
104
+
105
+ context 'when the file exceeds the max bson size' do
106
+
107
+ let ( :fs ) do
108
+ described_class . new ( authorized_client . database )
109
+ end
110
+
111
+ let ( :file ) do
112
+ str = 'y' * 16777216
113
+ Mongo ::Grid ::File . new ( str , :filename => 'large-file.txt' )
114
+ end
115
+
116
+ before do
117
+ fs . insert_one ( file )
118
+ end
119
+
120
+ after do
121
+ fs . files_collection . find . delete_many
122
+ fs . chunks_collection . find . delete_many
123
+ end
124
+
125
+ it 'successfully inserts the file' do
126
+ expect (
127
+ fs . find_one ( :filename => 'large-file.txt' ) . chunks
128
+ ) . to eq ( file . chunks )
129
+ end
130
+ end
104
131
end
105
132
106
133
describe '#delete_one' do
You can’t perform that action at this time.
0 commit comments