@@ -10966,6 +10966,61 @@ describe('model: populate:', function() {
10966
10966
assert . equal ( person . stories [ 0 ] . title , 'Casino Royale' ) ;
10967
10967
} ) ;
10968
10968
10969
+ it ( 'supports removing and then recreating populate virtual using schema clone (gh-13085)' , async function ( ) {
10970
+ const personSch = new mongoose . Schema (
10971
+ {
10972
+ firstName : { type : mongoose . SchemaTypes . String , required : true } ,
10973
+ surname : { type : mongoose . SchemaTypes . String , trim : true } ,
10974
+ nat : { type : mongoose . SchemaTypes . String , required : true , uppercase : true , minLength : 2 , maxLength : 2 }
10975
+ } ,
10976
+ { strict : true , timestamps : true }
10977
+ ) ;
10978
+ personSch . virtual ( 'nationality' , {
10979
+ localField : 'nat' ,
10980
+ foreignField : 'key' ,
10981
+ ref : 'Nat' ,
10982
+ justOne : true
10983
+ } ) ;
10984
+ let Person = db . model ( 'Person' , personSch . clone ( ) , 'people' ) ;
10985
+
10986
+ const natSch = new mongoose . Schema (
10987
+ {
10988
+ key : { type : mongoose . SchemaTypes . String , uppercase : true , index : true , minLength : 2 , maxLength : 2 } ,
10989
+ desc : { type : mongoose . SchemaTypes . String , trim : true }
10990
+ } ,
10991
+ { strict : true }
10992
+ ) ;
10993
+ const Nat = db . model ( 'Nat' , natSch ) ;
10994
+ let n = new Nat ( { key : 'ES' , desc : 'Spain' } ) ;
10995
+ await n . save ( ) ;
10996
+ n = new Nat ( { key : 'IT' , desc : 'Italy' } ) ;
10997
+ await n . save ( ) ;
10998
+ n = new Nat ( { key : 'FR' , desc : 'French' } ) ;
10999
+ await n . save ( ) ;
11000
+
11001
+ let p = new Person ( { firstName : 'Pepe' , surname : 'Pérez' , nat : 'it' } ) ;
11002
+ await p . save ( ) ;
11003
+ p = new Person ( { firstName : 'Paco' , surname : 'Matinez' , nat : 'es' } ) ;
11004
+ await p . save ( ) ;
11005
+ p = new Person ( { firstName : 'John' , surname : 'Doe' , nat : 'us' } ) ;
11006
+ await p . save ( ) ;
11007
+
11008
+ personSch . removeVirtual ( 'nationality' ) ;
11009
+ personSch . virtual ( 'nationality' , {
11010
+ localField : 'nat' ,
11011
+ foreignField : 'key' ,
11012
+ ref : 'Nat' ,
11013
+ justOne : true
11014
+ } ) ;
11015
+ Person = db . model ( 'Person' , personSch . clone ( ) , 'people' , { overwriteModels : true } ) ;
11016
+
11017
+ const peopleList = await Person . find ( ) .
11018
+ sort ( { firstName : 1 } ) .
11019
+ populate ( { path : 'nationality' , match : { desc : 'Spain' } } ) ;
11020
+ assert . deepStrictEqual ( peopleList . map ( p => p . nationality ?. key ) , [ undefined , 'ES' , undefined ] ) ;
11021
+
11022
+ } ) ;
11023
+
10969
11024
10970
11025
describe ( 'strictPopulate' , function ( ) {
10971
11026
it ( 'reports full path when throwing `strictPopulate` error with deep populate (gh-10923)' , async function ( ) {
0 commit comments