@@ -2711,7 +2711,7 @@ function _getPathsToValidate(doc, pathsToValidate, pathsToSkip, isNestedValidate
27112711
27122712 if ( ! isNestedValidate ) {
27132713 // If we're validating a subdocument, all this logic will run anyway on the top-level document, so skip for subdocuments
2714- const subdocs = doc . $getAllSubdocs ( ) ;
2714+ const subdocs = doc . $getAllSubdocs ( { useCache : true } ) ;
27152715 const modifiedPaths = doc . modifiedPaths ( ) ;
27162716 for ( const subdoc of subdocs ) {
27172717 if ( subdoc . $basePath ) {
@@ -3482,7 +3482,7 @@ Document.prototype.$__reset = function reset() {
34823482 let _this = this ;
34833483
34843484 // Skip for subdocuments
3485- const subdocs = ! this . $isSubdocument ? this . $getAllSubdocs ( ) : null ;
3485+ const subdocs = ! this . $isSubdocument ? this . $getAllSubdocs ( { useCache : true } ) : null ;
34863486 if ( subdocs && subdocs . length > 0 ) {
34873487 for ( const subdoc of subdocs ) {
34883488 subdoc . $__reset ( ) ;
@@ -3679,57 +3679,50 @@ Document.prototype.$__getArrayPathsToValidate = function() {
36793679 * @instance
36803680 */
36813681
3682- Document . prototype . $getAllSubdocs = function ( ) {
3682+ Document . prototype . $getAllSubdocs = function ( options ) {
3683+ if ( options ?. useCache && this . $__ . saveOptions ?. __subdocs ) {
3684+ return this . $__ . saveOptions . __subdocs ;
3685+ }
3686+
36833687 DocumentArray || ( DocumentArray = require ( './types/documentArray' ) ) ;
36843688 Embedded = Embedded || require ( './types/arraySubdocument' ) ;
36853689
3686- function docReducer ( doc , seed , path ) {
3687- let val = doc ;
3688- let isNested = false ;
3689- if ( path ) {
3690- if ( doc instanceof Document && doc [ documentSchemaSymbol ] . paths [ path ] ) {
3691- val = doc . _doc [ path ] ;
3692- } else if ( doc instanceof Document && doc [ documentSchemaSymbol ] . nested [ path ] ) {
3693- val = doc . _doc [ path ] ;
3694- isNested = true ;
3695- } else {
3696- val = doc [ path ] ;
3690+ const subDocs = [ ] ;
3691+ function getSubdocs ( doc ) {
3692+ const newSubdocs = [ ] ;
3693+ for ( const { path } of doc . $__schema . childSchemas ) {
3694+ const val = doc . $__getValue ( path ) ;
3695+ if ( val == null ) {
3696+ continue ;
36973697 }
3698- }
3699- if ( val instanceof Embedded ) {
3700- seed . push ( val ) ;
3701- } else if ( val instanceof Map ) {
3702- seed = Array . from ( val . keys ( ) ) . reduce ( function ( seed , path ) {
3703- return docReducer ( val . get ( path ) , seed , null ) ;
3704- } , seed ) ;
3705- } else if ( val && ! Array . isArray ( val ) && val . $isSingleNested ) {
3706- seed = Object . keys ( val . _doc ) . reduce ( function ( seed , path ) {
3707- return docReducer ( val , seed , path ) ;
3708- } , seed ) ;
3709- seed . push ( val ) ;
3710- } else if ( val && utils . isMongooseDocumentArray ( val ) ) {
3711- val . forEach ( function _docReduce ( doc ) {
3712- if ( ! doc || ! doc . _doc ) {
3713- return ;
3698+ if ( val . $__ ) {
3699+ newSubdocs . push ( val ) ;
3700+ }
3701+ if ( Array . isArray ( val ) ) {
3702+ for ( const el of val ) {
3703+ if ( el != null && el . $__ ) {
3704+ newSubdocs . push ( el ) ;
3705+ }
37143706 }
3715- seed = Object . keys ( doc . _doc ) . reduce ( function ( seed , path ) {
3716- return docReducer ( doc . _doc , seed , path ) ;
3717- } , seed ) ;
3718- if ( doc instanceof Embedded ) {
3719- seed . push ( doc ) ;
3707+ }
3708+ if ( val instanceof Map ) {
3709+ for ( const el of val . values ( ) ) {
3710+ if ( el != null && el . $__ ) {
3711+ newSubdocs . push ( el ) ;
3712+ }
37203713 }
3721- } ) ;
3722- } else if ( isNested && val != null ) {
3723- for ( const path of Object . keys ( val ) ) {
3724- docReducer ( val , seed , path ) ;
37253714 }
37263715 }
3727- return seed ;
3716+ for ( const subdoc of newSubdocs ) {
3717+ getSubdocs ( subdoc ) ;
3718+ }
3719+ subDocs . push ( ...newSubdocs ) ;
37283720 }
37293721
3730- const subDocs = [ ] ;
3731- for ( const path of Object . keys ( this . _doc ) ) {
3732- docReducer ( this , subDocs , path ) ;
3722+ getSubdocs ( this ) ;
3723+
3724+ if ( this . $__ . saveOptions ) {
3725+ this . $__ . saveOptions . __subdocs = subDocs ;
37333726 }
37343727
37353728 return subDocs ;
0 commit comments