@@ -1394,7 +1394,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
13941394
13951395 let didPopulate = false ;
13961396 if ( refMatches && val instanceof Document && ( ! val . $__ . wasPopulated || utils . deepEqual ( val . $__ . wasPopulated . value , val . _id ) ) ) {
1397- const unpopulatedValue = ( schema && schema . $isSingleNested ) ? schema . cast ( val , this ) : val . _id ;
1397+ const unpopulatedValue = ( schema && schema . $isSingleNested ) ? schema . cast ( val , this ) : val . _doc . _id ;
13981398 this . $populated ( path , unpopulatedValue , { [ populateModelSymbol ] : val . constructor } ) ;
13991399 val . $__ . wasPopulated = { value : unpopulatedValue } ;
14001400 didPopulate = true ;
@@ -1412,7 +1412,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
14121412 this . $populated ( path , val . map ( function ( v ) { return v . _id ; } ) , popOpts ) ;
14131413
14141414 for ( const doc of val ) {
1415- doc . $__ . wasPopulated = { value : doc . _id } ;
1415+ doc . $__ . wasPopulated = { value : doc . _doc . _id } ;
14161416 }
14171417 didPopulate = true ;
14181418 }
@@ -3840,7 +3840,7 @@ Document.prototype.$toObject = function(options, json) {
38403840 // _isNested will only be true if this is not the top level document, we
38413841 // should never depopulate the top-level document
38423842 if ( depopulate && options . _isNested && this . $__ . wasPopulated ) {
3843- return clone ( this . $__ . wasPopulated . value || this . _id , options ) ;
3843+ return clone ( this . $__ . wasPopulated . value || this . _doc . _id , options ) ;
38443844 }
38453845
38463846 // merge default options with input options.
0 commit comments