@@ -4275,11 +4275,20 @@ Model.populate = async function populate(docs, paths) {
42754275 }
42764276
42774277 // each path has its own query options and must be executed separately
4278- const promises = [ ] ;
4279- for ( const path of paths ) {
4280- promises . push ( _populatePath ( this , docs , path ) ) ;
4278+ if ( paths . ordered ) {
4279+ // Populate in series, primarily for transactions because MongoDB doesn't support multiple operations on
4280+ // one transaction in parallel.
4281+ for ( const path of paths ) {
4282+ await _populatePath ( this , docs , path ) ;
4283+ }
4284+ } else {
4285+ // By default, populate in parallel
4286+ const promises = [ ] ;
4287+ for ( const path of paths ) {
4288+ promises . push ( _populatePath ( this , docs , path ) ) ;
4289+ }
4290+ await Promise . all ( promises ) ;
42814291 }
4282- await Promise . all ( promises ) ;
42834292
42844293 return docs ;
42854294} ;
@@ -4399,12 +4408,22 @@ async function _populatePath(model, docs, populateOptions) {
43994408 return ;
44004409 }
44014410
4402- const promises = [ ] ;
4403- for ( const arr of params ) {
4404- promises . push ( _execPopulateQuery . apply ( null , arr ) . then ( valsFromDb => { vals = vals . concat ( valsFromDb ) ; } ) ) ;
4411+ if ( populateOptions . ordered ) {
4412+ // Populate in series, primarily for transactions because MongoDB doesn't support multiple operations on
4413+ // one transaction in parallel.
4414+ for ( const arr of params ) {
4415+ await _execPopulateQuery . apply ( null , arr ) . then ( valsFromDb => { vals = vals . concat ( valsFromDb ) ; } ) ;
4416+ }
4417+ } else {
4418+ // By default, populate in parallel
4419+ const promises = [ ] ;
4420+ for ( const arr of params ) {
4421+ promises . push ( _execPopulateQuery . apply ( null , arr ) . then ( valsFromDb => { vals = vals . concat ( valsFromDb ) ; } ) ) ;
4422+ }
4423+
4424+ await Promise . all ( promises ) ;
44054425 }
44064426
4407- await Promise . all ( promises ) ;
44084427
44094428 for ( const arr of params ) {
44104429 const mod = arr [ 0 ] ;
0 commit comments