@@ -3670,4 +3670,58 @@ describe('Query', function() {
3670
3670
assert . equal ( err . path , '$nor.0' ) ;
3671
3671
} ) ;
3672
3672
} ) ;
3673
+
3674
+ it ( 'handles push with array filters (gh-9977)' , function ( ) {
3675
+ const questionSchema = new Schema ( {
3676
+ question_type : { type : String , enum : [ 'mcq' , 'essay' ] }
3677
+ } , { discriminatorKey : 'question_type' } ) ;
3678
+
3679
+ const quizSchema = new Schema ( { quiz_title : String , questions : [ questionSchema ] } ) ;
3680
+ const Quiz = db . model ( 'Test' , quizSchema ) ;
3681
+
3682
+ const mcqQuestionSchema = new Schema ( {
3683
+ text : String ,
3684
+ choices : [ { choice_text : String , is_correct : Boolean } ]
3685
+ } ) ;
3686
+
3687
+ quizSchema . path ( 'questions' ) . discriminator ( 'mcq' , mcqQuestionSchema ) ;
3688
+
3689
+ const id1 = new mongoose . Types . ObjectId ( ) ;
3690
+ const id2 = new mongoose . Types . ObjectId ( ) ;
3691
+
3692
+ return co ( function * ( ) {
3693
+ let quiz = yield Quiz . create ( {
3694
+ quiz_title : 'quiz' ,
3695
+ questions : [
3696
+ {
3697
+ _id : id1 ,
3698
+ question_type : 'mcq' ,
3699
+ text : 'A or B?' ,
3700
+ choices : [
3701
+ { choice_text : 'A' , is_correct : false } ,
3702
+ { choice_text : 'B' , is_correct : true }
3703
+ ]
3704
+ } ,
3705
+ {
3706
+ _id : id2 ,
3707
+ question_type : 'mcq'
3708
+ }
3709
+ ]
3710
+ } ) ;
3711
+
3712
+ const filter = { questions : { $elemMatch : { _id : id2 , question_type : 'mcq' } } } ;
3713
+ yield Quiz . updateOne ( filter , {
3714
+ $push : {
3715
+ 'questions.$.choices' : {
3716
+ choice_text : 'choice 1' ,
3717
+ is_correct : false
3718
+ }
3719
+ }
3720
+ } ) ;
3721
+
3722
+ quiz = yield Quiz . findById ( quiz ) ;
3723
+ assert . equal ( quiz . questions [ 1 ] . choices . length , 1 ) ;
3724
+ assert . equal ( quiz . questions [ 1 ] . choices [ 0 ] . choice_text , 'choice 1' ) ;
3725
+ } ) ;
3726
+ } ) ;
3673
3727
} ) ;
0 commit comments