11import { 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' ;
38import { OrderedBulkOperation } from './bulk/ordered' ;
49import { UnorderedBulkOperation } from './bulk/unordered' ;
510import { ChangeStream , type ChangeStreamDocument , type ChangeStreamOptions } from './change_stream' ;
@@ -24,7 +29,6 @@ import type {
2429 WithoutId
2530} from './mongo_types' ;
2631import type { AggregateOptions } from './operations/aggregate' ;
27- import { BulkWriteOperation } from './operations/bulk_write' ;
2832import { CountOperation , type CountOptions } from './operations/count' ;
2933import {
3034 DeleteManyOperation ,
@@ -307,20 +311,17 @@ export class Collection<TSchema extends Document = Document> {
307311 if ( ! Array . isArray ( docs ) ) {
308312 throw new MongoInvalidArgumentError ( 'Argument "docs" must be an array of documents' ) ;
309313 }
314+ options = options ?? { } ;
310315
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 ;
319317
320318 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+ ) ;
322323 return {
323- acknowledged : writeConcern ?. w !== 0 ,
324+ acknowledged,
324325 insertedCount : res . insertedCount ,
325326 insertedIds : res . insertedIds
326327 } ;
@@ -361,14 +362,21 @@ export class Collection<TSchema extends Document = Document> {
361362 throw new MongoInvalidArgumentError ( 'Argument "operations" must be an array of documents' ) ;
362363 }
363364
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 } ) ;
372380 }
373381
374382 /**
0 commit comments