1
1
import { promisify } from 'util' ;
2
2
3
- import { type BSONSerializeOptions , type Document , ObjectId , resolveBSONOptions } from '../bson' ;
3
+ import { type BSONSerializeOptions , type Document , resolveBSONOptions } from '../bson' ;
4
4
import type { Collection } from '../collection' ;
5
5
import {
6
6
type AnyError ,
@@ -12,6 +12,7 @@ import {
12
12
} from '../error' ;
13
13
import type { Filter , OneOrMore , OptionalId , UpdateFilter , WithoutId } from '../mongo_types' ;
14
14
import type { CollationOptions , CommandOperationOptions } from '../operations/command' ;
15
+ import { maybeAddIdToDocuments } from '../operations/common_functions' ;
15
16
import { DeleteOperation , type DeleteStatement , makeDeleteStatement } from '../operations/delete' ;
16
17
import { executeOperation } from '../operations/execute_operation' ;
17
18
import { InsertOperation } from '../operations/insert' ;
@@ -917,7 +918,7 @@ export abstract class BulkOperationBase {
917
918
* Create a new OrderedBulkOperation or UnorderedBulkOperation instance
918
919
* @internal
919
920
*/
920
- constructor ( collection : Collection , options : BulkWriteOptions , isOrdered : boolean ) {
921
+ constructor ( private collection : Collection , options : BulkWriteOptions , isOrdered : boolean ) {
921
922
// determine whether bulkOperation is ordered or unordered
922
923
this . isOrdered = isOrdered ;
923
924
@@ -1032,9 +1033,9 @@ export abstract class BulkOperationBase {
1032
1033
* ```
1033
1034
*/
1034
1035
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
+ } ) ;
1038
1039
1039
1040
return this . addToOperationsList ( BatchType . INSERT , document ) ;
1040
1041
}
@@ -1093,21 +1094,16 @@ export abstract class BulkOperationBase {
1093
1094
throw new MongoInvalidArgumentError ( 'Operation must be an object with an operation key' ) ;
1094
1095
}
1095
1096
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
+ ? // NOTE: provided for legacy support, but this is a malformed operation
1101
+ ( op . insertOne as Document )
1102
+ : op . insertOne . document ;
1105
1103
1106
- if ( forceServerObjectId !== true && op . insertOne . document . _id == null ) {
1107
- op . insertOne . document . _id = new ObjectId ( ) ;
1108
- }
1104
+ maybeAddIdToDocuments ( this . collection , document , { forceServerObjectId } ) ;
1109
1105
1110
- return this . addToOperationsList ( BatchType . INSERT , op . insertOne . document ) ;
1106
+ return this . addToOperationsList ( BatchType . INSERT , document ) ;
1111
1107
}
1112
1108
1113
1109
if ( 'replaceOne' in op || 'updateOne' in op || 'updateMany' in op ) {
@@ -1268,6 +1264,18 @@ export abstract class BulkOperationBase {
1268
1264
batchType : BatchType ,
1269
1265
document : Document | UpdateStatement | DeleteStatement
1270
1266
) : this;
1267
+
1268
+ private shouldForceServerObjectId ( ) : boolean {
1269
+ if ( typeof this . s . options . forceServerObjectId === 'boolean' ) {
1270
+ return this . s . options . forceServerObjectId ;
1271
+ }
1272
+
1273
+ if ( typeof this . s . collection . s . db . options ?. forceServerObjectId === 'boolean' ) {
1274
+ return this . s . collection . s . db . options ?. forceServerObjectId ;
1275
+ }
1276
+
1277
+ return false ;
1278
+ }
1271
1279
}
1272
1280
1273
1281
Object . defineProperty ( BulkOperationBase . prototype , 'length' , {
@@ -1277,18 +1285,6 @@ Object.defineProperty(BulkOperationBase.prototype, 'length', {
1277
1285
}
1278
1286
} ) ;
1279
1287
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
-
1292
1288
function isInsertBatch ( batch : Batch ) : boolean {
1293
1289
return batch . batchType === BatchType . INSERT ;
1294
1290
}
0 commit comments