@@ -4500,6 +4500,57 @@ describe('model: populate:', function() {
4500
4500
catch ( done ) ;
4501
4501
} ) ;
4502
4502
4503
+ it ( 'in embedded array with sort (gh-10552)' , function ( ) {
4504
+ const AppMenuItemSchema = new Schema ( {
4505
+ appId : 'ObjectId' ,
4506
+ moduleId : Number ,
4507
+ title : String ,
4508
+ parent : {
4509
+ type : mongoose . ObjectId ,
4510
+ ref : 'AppMenuItem'
4511
+ } ,
4512
+ order : Number
4513
+ } ) ;
4514
+
4515
+ const moduleSchema = new Schema ( {
4516
+ _id : Number ,
4517
+ title : { type : String } ,
4518
+ hidden : { type : Boolean }
4519
+ } ) ;
4520
+
4521
+ moduleSchema . virtual ( 'menu' , {
4522
+ ref : 'Test1' ,
4523
+ localField : '_id' ,
4524
+ foreignField : 'moduleId' ,
4525
+ options : { sort : { title : 1 } }
4526
+ } ) ;
4527
+
4528
+ const appSchema = new Schema ( {
4529
+ modules : [ moduleSchema ]
4530
+ } ) ;
4531
+
4532
+ const App = db . model ( 'Test' , appSchema ) ;
4533
+ const AppMenuItem = db . model ( 'Test1' , AppMenuItemSchema ) ;
4534
+
4535
+ return co ( function * ( ) {
4536
+ let app = yield App . create ( { modules : [ { _id : 1 , title : 'File' } , { _id : 2 , title : 'Preferences' } ] } ) ;
4537
+ yield AppMenuItem . create ( [
4538
+ { title : 'Save' , moduleId : 1 } ,
4539
+ { title : 'Save As' , moduleId : 1 } ,
4540
+ { title : 'Undo' , moduleId : 2 } ,
4541
+ { title : 'Redo' , moduleId : 2 } ,
4542
+ ] ) ;
4543
+
4544
+ app = yield App . findById ( app ) . populate ( 'modules.menu' ) ;
4545
+ app = app . toObject ( { virtuals : true } ) ;
4546
+
4547
+ assert . equal ( app . modules . length , 2 ) ;
4548
+ assert . equal ( app . modules [ 0 ] . menu . length , 2 ) ;
4549
+ assert . deepEqual ( app . modules [ 0 ] . menu . map ( i => i . title ) , [ 'Save' , 'Save As' ] ) ;
4550
+ assert . deepEqual ( app . modules [ 1 ] . menu . map ( i => i . title ) , [ 'Redo' , 'Undo' ] ) ;
4551
+ } ) ;
4552
+ } ) ;
4553
+
4503
4554
it ( 'justOne option (gh-4263)' , function ( done ) {
4504
4555
const PersonSchema = new Schema ( {
4505
4556
name : String ,
0 commit comments