File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -5053,6 +5053,40 @@ describe('model: populate:', function() {
5053
5053
catch ( done ) ;
5054
5054
} ) ;
5055
5055
5056
+ it ( 'nested virtuals + doc.populate() (gh-5240)' , function ( done ) {
5057
+ var parentSchema = new Schema ( { name : String } ) ;
5058
+ var childSchema = new Schema ( {
5059
+ parentId : mongoose . Schema . Types . ObjectId
5060
+ } ) ;
5061
+ childSchema . virtual ( 'parent' , {
5062
+ ref : 'gh5240' ,
5063
+ localField : 'parentId' ,
5064
+ foreignField : '_id' ,
5065
+ justOne : true
5066
+ } ) ;
5067
+ var teamSchema = new Schema ( { people : [ childSchema ] } ) ;
5068
+
5069
+ var Parent = db . model ( 'gh5240' , parentSchema ) ;
5070
+ var Team = db . model ( 'gh5240_0' , teamSchema ) ;
5071
+
5072
+ Parent . create ( { name : 'Darth Vader' } ) .
5073
+ then ( function ( doc ) {
5074
+ return Team . create ( { people : [ { parentId : doc . _id } ] } ) ;
5075
+ } ) .
5076
+ then ( function ( team ) {
5077
+ return Team . findById ( team . _id ) ;
5078
+ } ) .
5079
+ then ( function ( team ) {
5080
+ return team . populate ( 'people.parent' ) . execPopulate ( ) ;
5081
+ } ) .
5082
+ then ( function ( team ) {
5083
+ team = team . toObject ( { virtuals : true } ) ;
5084
+ assert . equal ( team . people [ 0 ] . parent . name , 'Darth Vader' ) ;
5085
+ done ( ) ;
5086
+ } ) .
5087
+ catch ( done ) ;
5088
+ } ) ;
5089
+
5056
5090
it ( 'virtual populate in single nested doc (gh-4715)' , function ( done ) {
5057
5091
var someModelSchema = new mongoose . Schema ( {
5058
5092
name : String
You can’t perform that action at this time.
0 commit comments