@@ -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 ( ) ;
@@ -3672,64 +3672,58 @@ Document.prototype.$__getArrayPathsToValidate = function() {
36723672/**
36733673 * Get all subdocs (by bfs)
36743674 *
3675+ * @param {Object } [options] options. Currently for internal use.
36753676 * @return {Array }
36763677 * @api public
36773678 * @method $getAllSubdocs
36783679 * @memberOf Document
36793680 * @instance
36803681 */
36813682
3682- Document . prototype . $getAllSubdocs = function ( ) {
3683+ Document . prototype . $getAllSubdocs = function ( options ) {
3684+ if ( options ?. useCache && this . $__ . saveOptions ?. __subdocs ) {
3685+ return this . $__ . saveOptions . __subdocs ;
3686+ }
3687+
36833688 DocumentArray || ( DocumentArray = require ( './types/documentArray' ) ) ;
36843689 Embedded = Embedded || require ( './types/arraySubdocument' ) ;
36853690
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 ] ;
3691+ const subDocs = [ ] ;
3692+ function getSubdocs ( doc ) {
3693+ const newSubdocs = [ ] ;
3694+ for ( const { path } of doc . $__schema . childSchemas ) {
3695+ const val = doc . $__getValue ( path ) ;
3696+ if ( val == null ) {
3697+ continue ;
36973698 }
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 ;
3699+ if ( val . $__ ) {
3700+ newSubdocs . push ( val ) ;
3701+ }
3702+ if ( Array . isArray ( val ) ) {
3703+ for ( const el of val ) {
3704+ if ( el != null && el . $__ ) {
3705+ newSubdocs . push ( el ) ;
3706+ }
37143707 }
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 ) ;
3708+ }
3709+ if ( val instanceof Map ) {
3710+ for ( const el of val . values ( ) ) {
3711+ if ( el != null && el . $__ ) {
3712+ newSubdocs . push ( el ) ;
3713+ }
37203714 }
3721- } ) ;
3722- } else if ( isNested && val != null ) {
3723- for ( const path of Object . keys ( val ) ) {
3724- docReducer ( val , seed , path ) ;
37253715 }
37263716 }
3727- return seed ;
3717+ for ( const subdoc of newSubdocs ) {
3718+ getSubdocs ( subdoc ) ;
3719+ }
3720+ subDocs . push ( ...newSubdocs ) ;
37283721 }
37293722
3730- const subDocs = [ ] ;
3731- for ( const path of Object . keys ( this . _doc ) ) {
3732- docReducer ( this , subDocs , path ) ;
3723+ getSubdocs ( this ) ;
3724+
3725+ if ( this . $__ . saveOptions ) {
3726+ this . $__ . saveOptions . __subdocs = subDocs ;
37333727 }
37343728
37353729 return subDocs ;
0 commit comments