11import { promisify } from 'util' ;
22
3- import { type BSONSerializeOptions , type Document , ObjectId , resolveBSONOptions } from '../bson' ;
3+ import { type BSONSerializeOptions , type Document , resolveBSONOptions } from '../bson' ;
44import type { Collection } from '../collection' ;
55import {
66 type AnyError ,
@@ -12,6 +12,7 @@ import {
1212} from '../error' ;
1313import type { Filter , OneOrMore , OptionalId , UpdateFilter , WithoutId } from '../mongo_types' ;
1414import type { CollationOptions , CommandOperationOptions } from '../operations/command' ;
15+ import { maybeAddIdToDocuments } from '../operations/common_functions' ;
1516import { DeleteOperation , type DeleteStatement , makeDeleteStatement } from '../operations/delete' ;
1617import { executeOperation } from '../operations/execute_operation' ;
1718import { InsertOperation } from '../operations/insert' ;
@@ -917,7 +918,7 @@ export abstract class BulkOperationBase {
917918 * Create a new OrderedBulkOperation or UnorderedBulkOperation instance
918919 * @internal
919920 */
920- constructor ( collection : Collection , options : BulkWriteOptions , isOrdered : boolean ) {
921+ constructor ( private collection : Collection , options : BulkWriteOptions , isOrdered : boolean ) {
921922 // determine whether bulkOperation is ordered or unordered
922923 this . isOrdered = isOrdered ;
923924
@@ -1032,9 +1033,9 @@ export abstract class BulkOperationBase {
10321033 * ```
10331034 */
10341035 insert ( document : Document ) : BulkOperationBase {
1035- if ( document . _id == null && ! shouldForceServerObjectId ( this ) ) {
1036- document . _id = new ObjectId ( ) ;
1037- }
1036+ maybeAddIdToDocuments ( this . collection , document , {
1037+ forceServerObjectId : this . shouldForceServerObjectId ( )
1038+ } ) ;
10381039
10391040 return this . addToOperationsList ( BatchType . INSERT , document ) ;
10401041 }
@@ -1093,21 +1094,16 @@ export abstract class BulkOperationBase {
10931094 throw new MongoInvalidArgumentError ( 'Operation must be an object with an operation key' ) ;
10941095 }
10951096 if ( 'insertOne' in op ) {
1096- const forceServerObjectId = shouldForceServerObjectId ( this ) ;
1097- if ( op . insertOne && op . insertOne . document == null ) {
1098- // NOTE: provided for legacy support, but this is a malformed operation
1099- if ( forceServerObjectId !== true && ( op . insertOne as Document ) . _id == null ) {
1100- ( op . insertOne as Document ) . _id = new ObjectId ( ) ;
1101- }
1102-
1103- return this . addToOperationsList ( BatchType . INSERT , op . insertOne ) ;
1104- }
1097+ const forceServerObjectId = this . shouldForceServerObjectId ( ) ;
1098+ const document =
1099+ op . insertOne && op . insertOne . document == null
1100+ ? // TODO(NODE-6003): remove support for omitting the `documents` subdocument in bulk inserts
1101+ ( op . insertOne as Document )
1102+ : op . insertOne . document ;
11051103
1106- if ( forceServerObjectId !== true && op . insertOne . document . _id == null ) {
1107- op . insertOne . document . _id = new ObjectId ( ) ;
1108- }
1104+ maybeAddIdToDocuments ( this . collection , document , { forceServerObjectId } ) ;
11091105
1110- return this . addToOperationsList ( BatchType . INSERT , op . insertOne . document ) ;
1106+ return this . addToOperationsList ( BatchType . INSERT , document ) ;
11111107 }
11121108
11131109 if ( 'replaceOne' in op || 'updateOne' in op || 'updateMany' in op ) {
@@ -1268,6 +1264,13 @@ export abstract class BulkOperationBase {
12681264 batchType : BatchType ,
12691265 document : Document | UpdateStatement | DeleteStatement
12701266 ) : this;
1267+
1268+ private shouldForceServerObjectId ( ) : boolean {
1269+ return (
1270+ this . s . options . forceServerObjectId === true ||
1271+ this . s . collection . s . db . options ?. forceServerObjectId === true
1272+ ) ;
1273+ }
12711274}
12721275
12731276Object . defineProperty ( BulkOperationBase . prototype , 'length' , {
@@ -1277,18 +1280,6 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
12771280 }
12781281} ) ;
12791282
1280- function shouldForceServerObjectId ( bulkOperation : BulkOperationBase ) : boolean {
1281- if ( typeof bulkOperation . s . options . forceServerObjectId === 'boolean' ) {
1282- return bulkOperation . s . options . forceServerObjectId ;
1283- }
1284-
1285- if ( typeof bulkOperation . s . collection . s . db . options ?. forceServerObjectId === 'boolean' ) {
1286- return bulkOperation . s . collection . s . db . options ?. forceServerObjectId ;
1287- }
1288-
1289- return false ;
1290- }
1291-
12921283function isInsertBatch ( batch : Batch ) : boolean {
12931284 return batch . batchType === BatchType . INSERT ;
12941285}
0 commit comments