@@ -3430,6 +3430,13 @@ Model.$__insertMany = function(arr, options, callback) {
3430
3430
const results = ordered ? null : new Array ( arr . length ) ;
3431
3431
const toExecute = arr . map ( ( doc , index ) =>
3432
3432
callback => {
3433
+ // If option `lean` is set to true bypass validation and hydration
3434
+ if ( lean ) {
3435
+ // we have to execute callback at the nextTick to be compatible
3436
+ // with parallelLimit, as `results` variable has TDZ issue if we
3437
+ // execute the callback synchronously
3438
+ return immediate ( ( ) => callback ( null , doc ) ) ;
3439
+ }
3433
3440
if ( ! ( doc instanceof _this ) ) {
3434
3441
try {
3435
3442
doc = new _this ( doc ) ;
@@ -3440,13 +3447,6 @@ Model.$__insertMany = function(arr, options, callback) {
3440
3447
if ( options . session != null ) {
3441
3448
doc . $session ( options . session ) ;
3442
3449
}
3443
- // If option `lean` is set to true bypass validation
3444
- if ( lean ) {
3445
- // we have to execute callback at the nextTick to be compatible
3446
- // with parallelLimit, as `results` variable has TDZ issue if we
3447
- // execute the callback synchronously
3448
- return immediate ( ( ) => callback ( null , doc ) ) ;
3449
- }
3450
3450
doc . $validate ( { __noPromise : true } , function ( error ) {
3451
3451
if ( error ) {
3452
3452
// Option `ordered` signals that insert should be continued after reaching
@@ -3510,7 +3510,7 @@ Model.$__insertMany = function(arr, options, callback) {
3510
3510
callback ( null , [ ] ) ;
3511
3511
return ;
3512
3512
}
3513
- const docObjects = docAttributes . map ( function ( doc ) {
3513
+ const docObjects = lean ? docAttributes : docAttributes . map ( function ( doc ) {
3514
3514
if ( doc . $__schema . options . versionKey ) {
3515
3515
doc [ doc . $__schema . options . versionKey ] = 0 ;
3516
3516
}
@@ -3572,6 +3572,9 @@ Model.$__insertMany = function(arr, options, callback) {
3572
3572
return ! isErrored ;
3573
3573
} ) .
3574
3574
map ( function setIsNewForInsertedDoc ( doc ) {
3575
+ if ( lean ) {
3576
+ return doc ;
3577
+ }
3575
3578
doc . $__reset ( ) ;
3576
3579
_setIsNew ( doc , false ) ;
3577
3580
return doc ;
@@ -3588,9 +3591,11 @@ Model.$__insertMany = function(arr, options, callback) {
3588
3591
return ;
3589
3592
}
3590
3593
3591
- for ( const attribute of docAttributes ) {
3592
- attribute . $__reset ( ) ;
3593
- _setIsNew ( attribute , false ) ;
3594
+ if ( ! lean ) {
3595
+ for ( const attribute of docAttributes ) {
3596
+ attribute . $__reset ( ) ;
3597
+ _setIsNew ( attribute , false ) ;
3598
+ }
3594
3599
}
3595
3600
3596
3601
if ( rawResult ) {
0 commit comments