@@ -3393,7 +3393,7 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3393
3393
return bulkWriteResult ;
3394
3394
}
3395
3395
3396
- const validations = ops . map ( op => castBulkWrite ( this , op , options ) ) ;
3396
+ const validations = options ?. _skipCastBulkWrite ? [ ] : ops . map ( op => castBulkWrite ( this , op , options ) ) ;
3397
3397
const asyncLocalStorage = this . db . base . transactionAsyncLocalStorage ?. getStore ( ) ;
3398
3398
if ( ( ! options || ! options . hasOwnProperty ( 'session' ) ) && asyncLocalStorage ?. session != null ) {
3399
3399
options = { ...options , session : asyncLocalStorage . session } ;
@@ -3430,6 +3430,9 @@ Model.bulkWrite = async function bulkWrite(ops, options) {
3430
3430
let validationErrors = [ ] ;
3431
3431
const results = [ ] ;
3432
3432
await new Promise ( ( resolve ) => {
3433
+ if ( validations . length === 0 ) {
3434
+ return resolve ( ) ;
3435
+ }
3433
3436
for ( let i = 0 ; i < validations . length ; ++ i ) {
3434
3437
validations [ i ] ( ( err ) => {
3435
3438
if ( err == null ) {
@@ -3567,8 +3570,9 @@ Model.bulkSave = async function bulkSave(documents, options) {
3567
3570
3568
3571
await Promise . all ( documents . map ( doc => buildPreSavePromise ( doc , options ) ) ) ;
3569
3572
3570
- const writeOperations = this . buildBulkWriteOperations ( documents , { skipValidation : true , timestamps : options . timestamps } ) ;
3571
- const { bulkWriteResult, bulkWriteError } = await this . bulkWrite ( writeOperations , { skipValidation : true , ...options } ) . then (
3573
+ const writeOperations = this . buildBulkWriteOperations ( documents , options ) ;
3574
+ const opts = { skipValidation : true , _skipCastBulkWrite : true , ...options } ;
3575
+ const { bulkWriteResult, bulkWriteError } = await this . bulkWrite ( writeOperations , opts ) . then (
3572
3576
( res ) => ( { bulkWriteResult : res , bulkWriteError : null } ) ,
3573
3577
( err ) => ( { bulkWriteResult : null , bulkWriteError : err } )
3574
3578
) ;
@@ -3867,26 +3871,25 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3867
3871
}
3868
3872
3869
3873
setDefaultOptions ( ) ;
3870
- const discriminatorKey = this . schema . options . discriminatorKey ;
3871
3874
3872
- const writeOperations = documents . reduce ( ( accumulator , document , i ) => {
3875
+ const writeOperations = documents . map ( ( document , i ) => {
3873
3876
if ( ! options . skipValidation ) {
3874
3877
if ( ! ( document instanceof Document ) ) {
3875
3878
throw new Error ( `documents.${ i } was not a mongoose document, documents must be an array of mongoose documents (instanceof mongoose.Document).` ) ;
3876
3879
}
3877
- const validationError = document . validateSync ( ) ;
3878
- if ( validationError ) {
3879
- throw validationError ;
3880
+ if ( options . validateBeforeSave == null || options . validateBeforeSave ) {
3881
+ const err = document . validateSync ( ) ;
3882
+ if ( err != null ) {
3883
+ throw err ;
3884
+ }
3880
3885
}
3881
3886
}
3882
3887
3883
3888
const isANewDocument = document . isNew ;
3884
3889
if ( isANewDocument ) {
3885
3890
const writeOperation = { insertOne : { document } } ;
3886
3891
utils . injectTimestampsOption ( writeOperation . insertOne , options . timestamps ) ;
3887
- accumulator . push ( writeOperation ) ;
3888
-
3889
- return accumulator ;
3892
+ return writeOperation ;
3890
3893
}
3891
3894
3892
3895
const delta = document . $__delta ( ) ;
@@ -3909,22 +3912,14 @@ Model.buildBulkWriteOperations = function buildBulkWriteOperations(documents, op
3909
3912
}
3910
3913
}
3911
3914
3912
- // Set the discriminator key, so bulk write casting knows which
3913
- // schema to use re: gh-13907
3914
- if ( document [ discriminatorKey ] != null && ! ( discriminatorKey in where ) ) {
3915
- where [ discriminatorKey ] = document [ discriminatorKey ] ;
3916
- }
3917
-
3918
3915
document . $__version ( where , delta ) ;
3919
3916
const writeOperation = { updateOne : { filter : where , update : changes } } ;
3920
3917
utils . injectTimestampsOption ( writeOperation . updateOne , options . timestamps ) ;
3921
- accumulator . push ( writeOperation ) ;
3922
-
3923
- return accumulator ;
3918
+ return writeOperation ;
3924
3919
}
3925
3920
3926
- return accumulator ;
3927
- } , [ ] ) ;
3921
+ return null ;
3922
+ } ) . filter ( op => op !== null ) ;
3928
3923
3929
3924
return writeOperations ;
3930
3925
0 commit comments