1
1
import { type BSONSerializeOptions , type Document , resolveBSONOptions } from './bson' ;
2
- import type { AnyBulkWriteOperation , BulkWriteOptions , BulkWriteResult } from './bulk/common' ;
2
+ import type {
3
+ AnyBulkWriteOperation ,
4
+ BulkOperationBase ,
5
+ BulkWriteOptions ,
6
+ BulkWriteResult
7
+ } from './bulk/common' ;
3
8
import { OrderedBulkOperation } from './bulk/ordered' ;
4
9
import { UnorderedBulkOperation } from './bulk/unordered' ;
5
10
import { ChangeStream , type ChangeStreamDocument , type ChangeStreamOptions } from './change_stream' ;
@@ -24,7 +29,6 @@ import type {
24
29
WithoutId
25
30
} from './mongo_types' ;
26
31
import type { AggregateOptions } from './operations/aggregate' ;
27
- import { BulkWriteOperation } from './operations/bulk_write' ;
28
32
import { CountOperation , type CountOptions } from './operations/count' ;
29
33
import {
30
34
DeleteManyOperation ,
@@ -307,20 +311,17 @@ export class Collection<TSchema extends Document = Document> {
307
311
if ( ! Array . isArray ( docs ) ) {
308
312
throw new MongoInvalidArgumentError ( 'Argument "docs" must be an array of documents' ) ;
309
313
}
314
+ options = options ?? { } ;
310
315
311
- const writeConcern = WriteConcern . fromOptions ( options ) ;
312
- const bulkWriteOperation = new BulkWriteOperation (
313
- this as unknown as Collection < Document > ,
314
- docs . map ( document => ( {
315
- insertOne : { document }
316
- } ) ) ,
317
- options ?? { }
318
- ) ;
316
+ const acknowledged = WriteConcern . fromOptions ( options ) ?. w !== 0 ;
319
317
320
318
try {
321
- const res = await executeOperation ( this . client , bulkWriteOperation ) ;
319
+ const res = await this . bulkWrite (
320
+ docs . map ( doc => ( { insertOne : { document : doc } } ) ) ,
321
+ options
322
+ ) ;
322
323
return {
323
- acknowledged : writeConcern ?. w !== 0 ,
324
+ acknowledged,
324
325
insertedCount : res . insertedCount ,
325
326
insertedIds : res . insertedIds
326
327
} ;
@@ -361,14 +362,21 @@ export class Collection<TSchema extends Document = Document> {
361
362
throw new MongoInvalidArgumentError ( 'Argument "operations" must be an array of documents' ) ;
362
363
}
363
364
364
- return await executeOperation (
365
- this . client ,
366
- new BulkWriteOperation (
367
- this as TODO_NODE_3286 ,
368
- operations ,
369
- resolveOptions ( this , options ?? { ordered : true } )
370
- )
371
- ) ;
365
+ options = options ?? { } ;
366
+
367
+ // Create the bulk operation
368
+ const bulk : BulkOperationBase =
369
+ options . ordered === false
370
+ ? this . initializeUnorderedBulkOp ( options )
371
+ : this . initializeOrderedBulkOp ( options ) ;
372
+
373
+ // for each op go through and add to the bulk
374
+ for ( let i = 0 ; i < operations . length ; i ++ ) {
375
+ bulk . raw ( operations [ i ] ) ;
376
+ }
377
+
378
+ // Execute the bulk
379
+ return await bulk . execute ( { ...options } ) ;
372
380
}
373
381
374
382
/**
0 commit comments