@@ -13,8 +13,8 @@ import {
13
13
} from '../utils' ;
14
14
import { executeOperation } from '../operations/execute_operation' ;
15
15
import { InsertOperation } from '../operations/insert' ;
16
- import { UpdateOperation } from '../operations/update' ;
17
- import { DeleteOperation } from '../operations/delete' ;
16
+ import { UpdateOperation , UpdateStatement } from '../operations/update' ;
17
+ import { DeleteOperation , DeleteStatement } from '../operations/delete' ;
18
18
import { WriteConcern } from '../write_concern' ;
19
19
import type { Collection } from '../collection' ;
20
20
import type { Topology } from '../sdam/topology' ;
@@ -28,7 +28,7 @@ const WRITE_CONCERN_ERROR = 64;
28
28
export const BatchType = {
29
29
INSERT : 1 ,
30
30
UPDATE : 2 ,
31
- REMOVE : 3
31
+ DELETE : 3
32
32
} as const ;
33
33
34
34
/** @public */
@@ -139,12 +139,12 @@ export interface BulkResult {
139
139
*
140
140
* @public
141
141
*/
142
- export class Batch {
142
+ export class Batch < T = Document > {
143
143
originalZeroIndex : number ;
144
144
currentIndex : number ;
145
145
originalIndexes : number [ ] ;
146
146
batchType : BatchTypeId ;
147
- operations : Document [ ] ;
147
+ operations : T [ ] ;
148
148
size : number ;
149
149
sizeBytes : number ;
150
150
@@ -483,12 +483,12 @@ function mergeBatchResults(
483
483
}
484
484
485
485
// If we have an insert Batch type
486
- if ( batch . batchType === BatchType . INSERT && result . n ) {
486
+ if ( isInsertBatch ( batch ) && result . n ) {
487
487
bulkResult . nInserted = bulkResult . nInserted + result . n ;
488
488
}
489
489
490
490
// If we have an insert Batch type
491
- if ( batch . batchType === BatchType . REMOVE && result . n ) {
491
+ if ( isDeleteBatch ( batch ) && result . n ) {
492
492
bulkResult . nRemoved = bulkResult . nRemoved + result . n ;
493
493
}
494
494
@@ -514,7 +514,7 @@ function mergeBatchResults(
514
514
}
515
515
516
516
// If we have an update Batch type
517
- if ( batch . batchType === BatchType . UPDATE && result . n ) {
517
+ if ( isUpdateBatch ( batch ) && result . n ) {
518
518
const nModified = result . nModified ;
519
519
bulkResult . nUpserted = bulkResult . nUpserted + nUpserted ;
520
520
bulkResult . nMatched = bulkResult . nMatched + ( result . n - nUpserted ) ;
@@ -605,30 +605,30 @@ function executeCommands(
605
605
}
606
606
607
607
if ( finalOptions . retryWrites ) {
608
- if ( batch . batchType === BatchType . UPDATE ) {
608
+ if ( isUpdateBatch ( batch ) ) {
609
609
finalOptions . retryWrites = finalOptions . retryWrites && ! batch . operations . some ( op => op . multi ) ;
610
610
}
611
611
612
- if ( batch . batchType === BatchType . REMOVE ) {
612
+ if ( isDeleteBatch ( batch ) ) {
613
613
finalOptions . retryWrites =
614
614
finalOptions . retryWrites && ! batch . operations . some ( op => op . limit === 0 ) ;
615
615
}
616
616
}
617
617
618
618
try {
619
- if ( batch . batchType === BatchType . INSERT ) {
619
+ if ( isInsertBatch ( batch ) ) {
620
620
executeOperation (
621
621
bulkOperation . s . topology ,
622
622
new InsertOperation ( bulkOperation . s . namespace , batch . operations , finalOptions ) ,
623
623
resultHandler
624
624
) ;
625
- } else if ( batch . batchType === BatchType . UPDATE ) {
625
+ } else if ( isUpdateBatch ( batch ) ) {
626
626
executeOperation (
627
627
bulkOperation . s . topology ,
628
628
new UpdateOperation ( bulkOperation . s . namespace , batch . operations , finalOptions ) ,
629
629
resultHandler
630
630
) ;
631
- } else if ( batch . batchType === BatchType . REMOVE ) {
631
+ } else if ( isDeleteBatch ( batch ) ) {
632
632
executeOperation (
633
633
bulkOperation . s . topology ,
634
634
new DeleteOperation ( bulkOperation . s . namespace , batch . operations , finalOptions ) ,
@@ -811,12 +811,12 @@ export class FindOperators {
811
811
812
812
/** Add a delete one operation to the bulk operation */
813
813
deleteOne ( ) : BulkOperationBase {
814
- return this . bulkOperation . addToOperationsList ( BatchType . REMOVE , this . makeDeleteDocument ( 1 ) ) ;
814
+ return this . bulkOperation . addToOperationsList ( BatchType . DELETE , this . makeDeleteDocument ( 1 ) ) ;
815
815
}
816
816
817
817
/** Add a delete many operation to the bulk operation */
818
818
delete ( ) : BulkOperationBase {
819
- return this . bulkOperation . addToOperationsList ( BatchType . REMOVE , this . makeDeleteDocument ( 0 ) ) ;
819
+ return this . bulkOperation . addToOperationsList ( BatchType . DELETE , this . makeDeleteDocument ( 0 ) ) ;
820
820
}
821
821
822
822
removeOne ( ) : BulkOperationBase {
@@ -1059,7 +1059,7 @@ export abstract class BulkOperationBase {
1059
1059
* bulkOp.find({ h: 8 }).delete();
1060
1060
*
1061
1061
* // Add a replaceOne
1062
- * bulkOp.find({ i: 9 }).replaceOne({ j: 10 });
1062
+ * bulkOp.find({ i: 9 }).replaceOne({writeConcern: { j: 10 } });
1063
1063
*
1064
1064
* // Update using a pipeline (requires Mongodb 4.2 or higher)
1065
1065
* bulk.find({ k: 11, y: { $exists: true }, z: { $exists: true } }).updateOne([
@@ -1143,28 +1143,28 @@ export abstract class BulkOperationBase {
1143
1143
1144
1144
if ( 'removeOne' in op ) {
1145
1145
return this . addToOperationsList (
1146
- BatchType . REMOVE ,
1146
+ BatchType . DELETE ,
1147
1147
makeDeleteStatement ( this . s . topology , op . removeOne , false )
1148
1148
) ;
1149
1149
}
1150
1150
1151
1151
if ( 'removeMany' in op ) {
1152
1152
return this . addToOperationsList (
1153
- BatchType . REMOVE ,
1153
+ BatchType . DELETE ,
1154
1154
makeDeleteStatement ( this . s . topology , op . removeMany , true )
1155
1155
) ;
1156
1156
}
1157
1157
1158
1158
if ( 'deleteOne' in op ) {
1159
1159
return this . addToOperationsList (
1160
- BatchType . REMOVE ,
1160
+ BatchType . DELETE ,
1161
1161
makeDeleteStatement ( this . s . topology , op . deleteOne , false )
1162
1162
) ;
1163
1163
}
1164
1164
1165
1165
if ( 'deleteMany' in op ) {
1166
1166
return this . addToOperationsList (
1167
- BatchType . REMOVE ,
1167
+ BatchType . DELETE ,
1168
1168
makeDeleteStatement ( this . s . topology , op . deleteMany , true )
1169
1169
) ;
1170
1170
}
@@ -1303,24 +1303,6 @@ function shouldForceServerObjectId(bulkOperation: BulkOperationBase): boolean {
1303
1303
return false ;
1304
1304
}
1305
1305
1306
- /** @public */
1307
- export interface UpdateStatement {
1308
- /** The query that matches documents to update. */
1309
- q : Document ;
1310
- /** The modifications to apply. */
1311
- u : Document | Document [ ] ;
1312
- /** If true, perform an insert if no documents match the query. */
1313
- upsert ?: boolean ;
1314
- /** If true, updates all documents that meet the query criteria. */
1315
- multi ?: boolean ;
1316
- /** Specifies the collation to use for the operation. */
1317
- collation ?: CollationOptions ;
1318
- /** An array of filter documents that determines which array elements to modify for an update operation on an array field. */
1319
- arrayFilters ?: Document [ ] ;
1320
- /** A document or string that specifies the index to use to support the query predicate. */
1321
- hint ?: Hint ;
1322
- }
1323
-
1324
1306
function makeUpdateStatement (
1325
1307
topology : Topology ,
1326
1308
model : ReplaceOneModel | UpdateOneModel | UpdateManyModel ,
@@ -1370,18 +1352,6 @@ function isUpdateStatement(model: Document): model is UpdateStatement {
1370
1352
return 'q' in model ;
1371
1353
}
1372
1354
1373
- /** @public */
1374
- export interface DeleteStatement {
1375
- /** The query that matches documents to delete. */
1376
- q : Document ;
1377
- /** The number of matching documents to delete. */
1378
- limit : number ;
1379
- /** Specifies the collation to use for the operation. */
1380
- collation ?: CollationOptions ;
1381
- /** A document or string that specifies the index to use to support the query predicate. */
1382
- hint ?: Hint ;
1383
- }
1384
-
1385
1355
function makeDeleteStatement (
1386
1356
topology : Topology ,
1387
1357
model : DeleteOneModel | DeleteManyModel ,
@@ -1420,3 +1390,15 @@ function makeDeleteStatement(
1420
1390
function isDeleteStatement ( model : Document ) : model is DeleteStatement {
1421
1391
return 'q' in model ;
1422
1392
}
1393
+
1394
+ function isInsertBatch ( batch : Batch ) : boolean {
1395
+ return batch . batchType === BatchType . INSERT ;
1396
+ }
1397
+
1398
+ function isUpdateBatch ( batch : Batch ) : batch is Batch < UpdateStatement > {
1399
+ return batch . batchType === BatchType . UPDATE ;
1400
+ }
1401
+
1402
+ function isDeleteBatch ( batch : Batch ) : batch is Batch < DeleteStatement > {
1403
+ return batch . batchType === BatchType . DELETE ;
1404
+ }
0 commit comments