@@ -139,19 +139,25 @@ class UnorderedBulkOperation extends BulkOperationBase {
139
139
options = ret . options ;
140
140
callback = ret . callback ;
141
141
142
- return executeOperation ( this . s . topology , executeBatches , [ this , options , callback ] ) ;
142
+ return executeOperation ( this . s . topology , executeCommands , [ this , options , callback ] ) ;
143
143
}
144
144
}
145
145
146
146
/**
147
- * Execute the command
147
+ * Execute next write command in a chain
148
148
*
149
- * @param {UnorderedBulkOperation } bulkOperation
150
- * @param {object } batch
149
+ * @param {OrderedBulkOperation } bulkOperation
151
150
* @param {object } options
152
151
* @param {function } callback
153
152
*/
154
- function executeBatch ( bulkOperation , batch , options , callback ) {
153
+ function executeCommands ( bulkOperation , options , callback ) {
154
+ if ( bulkOperation . s . batches . length === 0 ) {
155
+ return handleCallback ( callback , null , new BulkWriteResult ( bulkOperation . s . bulkResult ) ) ;
156
+ }
157
+
158
+ // Ordered execution of the command
159
+ const batch = bulkOperation . s . batches . shift ( ) ;
160
+
155
161
function resultHandler ( err , result ) {
156
162
// Error is a driver related error not a bulk op error, terminate
157
163
if ( ( ( err && err . driver ) || ( err && err . message ) ) && ! ( err instanceof MongoWriteConcernError ) ) {
@@ -161,54 +167,23 @@ function executeBatch(bulkOperation, batch, options, callback) {
161
167
// If we have and error
162
168
if ( err ) err . ok = 0 ;
163
169
if ( err instanceof MongoWriteConcernError ) {
164
- return handleMongoWriteConcernError ( batch , bulkOperation . s . bulkResult , false , err , callback ) ;
170
+ return handleMongoWriteConcernError ( batch , bulkOperation . s . bulkResult , true , err , callback ) ;
165
171
}
166
- handleCallback (
167
- callback ,
168
- null ,
169
- mergeBatchResults ( false , batch , bulkOperation . s . bulkResult , err , result )
170
- ) ;
171
- }
172
172
173
- bulkOperation . finalOptionsHandler ( { options, batch, resultHandler } , callback ) ;
174
- }
173
+ // Merge the results together
174
+ const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
175
+ const mergeResult = mergeBatchResults ( true , batch , bulkOperation . s . bulkResult , err , result ) ;
176
+ if ( mergeResult != null ) {
177
+ return handleCallback ( callback , null , writeResult ) ;
178
+ }
175
179
176
- /**
177
- * Execute all the commands
178
- *
179
- * @param {UnorderedBulkOperation } bulkOperation
180
- * @param {object } options
181
- * @param {function } callback
182
- */
183
- function executeBatches ( bulkOperation , options , callback ) {
184
- let numberOfCommandsToExecute = bulkOperation . s . batches . length ;
185
- let hasErrored = false ;
186
- // Execute over all the batches
187
- for ( let i = 0 ; i < bulkOperation . s . batches . length ; i ++ ) {
188
- executeBatch ( bulkOperation , bulkOperation . s . batches [ i ] , options , function ( err ) {
189
- if ( hasErrored ) {
190
- return ;
191
- }
192
-
193
- if ( err ) {
194
- hasErrored = true ;
195
- return handleCallback ( callback , err ) ;
196
- }
197
- // Count down the number of commands left to execute
198
- numberOfCommandsToExecute = numberOfCommandsToExecute - 1 ;
199
-
200
- // Execute
201
- if ( numberOfCommandsToExecute === 0 ) {
202
- // Driver level error
203
- if ( err ) return handleCallback ( callback , err ) ;
204
-
205
- const writeResult = new BulkWriteResult ( bulkOperation . s . bulkResult ) ;
206
- if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
207
-
208
- return handleCallback ( callback , null , writeResult ) ;
209
- }
210
- } ) ;
180
+ if ( bulkOperation . handleWriteError ( callback , writeResult ) ) return ;
181
+
182
+ // Execute the next command in line
183
+ executeCommands ( bulkOperation , options , callback ) ;
211
184
}
185
+
186
+ bulkOperation . finalOptionsHandler ( { options, batch, resultHandler } , callback ) ;
212
187
}
213
188
214
189
/**
0 commit comments