@@ -934,4 +934,64 @@ describe('document.populate', function() {
934
934
assert . ok ( foundBook . populated ( 'authorId' ) ) ;
935
935
assert . ok ( foundBook . authorId . populated ( 'websiteId' ) ) ;
936
936
} ) ;
937
+
938
+ it ( 'works when populating a nested document inside an array parent (gh-14435)' , async function ( ) {
939
+ const CodeSchema = new Schema ( {
940
+ code : String
941
+ } ) ;
942
+
943
+ const UserSchema = new Schema ( {
944
+ username : String ,
945
+ extras : [
946
+ new Schema ( {
947
+ config : new Schema ( {
948
+ paymentConfiguration : {
949
+ paymentMethods : [
950
+ {
951
+ type : Schema . Types . ObjectId ,
952
+ ref : 'Code'
953
+ }
954
+ ]
955
+ }
956
+ } )
957
+ } )
958
+ ]
959
+ } ) ;
960
+
961
+ const Code = db . model ( 'Code' , CodeSchema ) ;
962
+ const CodeUser = db . model ( 'CodeUser' , UserSchema ) ;
963
+
964
+ const code = await Code . create ( {
965
+ code : 'test code'
966
+ } ) ;
967
+
968
+ await CodeUser . create ( {
969
+ username : 'TestUser' ,
970
+ extras : [
971
+ {
972
+ config : {
973
+ paymentConfiguration : {
974
+ paymentMethods : [ code . _id ]
975
+ }
976
+ }
977
+ }
978
+ ]
979
+ } ) ;
980
+
981
+ const codeUser = await CodeUser . findOne ( { username : 'TestUser' } ) . populate (
982
+ 'extras.config.paymentConfiguration.paymentMethods'
983
+ ) ;
984
+
985
+ assert . ok ( codeUser . username ) ;
986
+ assert . strictEqual ( codeUser . username , 'TestUser' ) ;
987
+ assert . ok ( codeUser . extras ) ;
988
+ assert . strictEqual ( codeUser . extras . length , 1 ) ;
989
+ assert . ok ( codeUser . extras [ 0 ] ) ;
990
+ assert . ok ( codeUser . extras [ 0 ] . config ) ;
991
+ assert . ok ( codeUser . extras [ 0 ] . config . paymentConfiguration ) ;
992
+ assert . ok ( codeUser . extras [ 0 ] . config . paymentConfiguration . paymentMethods ) ;
993
+ assert . strictEqual ( codeUser . extras [ 0 ] . config . paymentConfiguration . paymentMethods . length , 1 ) ;
994
+ assert . deepStrictEqual ( codeUser . extras [ 0 ] . config . paymentConfiguration . paymentMethods [ 0 ] . _id , code . _id ) ;
995
+ assert . strictEqual ( codeUser . extras [ 0 ] . config . paymentConfiguration . paymentMethods [ 0 ] . code , 'test code' ) ;
996
+ } ) ;
937
997
} ) ;
0 commit comments