@@ -157,6 +157,72 @@ describe('model: findOneAndUpdate:', function(){
157
157
} ) ;
158
158
} ) ;
159
159
160
+ describe ( 'will correctly' , function ( ) {
161
+ var db , ItemParentModel , ItemChildModel ;
162
+
163
+ before ( function ( ) {
164
+ db = start ( ) ;
165
+ var itemSpec = new Schema ( {
166
+ item_id : {
167
+ type : ObjectId , required : true , default : function ( ) { return new DocumentObjectId ( ) ; }
168
+ } ,
169
+ address : {
170
+ street : String ,
171
+ zipcode : String
172
+ } ,
173
+ age : Number
174
+ } , { _id : false } ) ;
175
+ var itemSchema = new Schema ( {
176
+ items : [ itemSpec ] ,
177
+ } ) ;
178
+ ItemParentModel = db . model ( 'ItemParentModel' , itemSchema ) ;
179
+ ItemChildModel = db . model ( 'ItemChildModel' , itemSpec ) ;
180
+ } ) ;
181
+
182
+ after ( function ( ) {
183
+ db . close ( ) ;
184
+ } ) ;
185
+
186
+ it ( 'update subdocument in array item' , function ( done ) {
187
+ var item1 = new ItemChildModel ( {
188
+ address : {
189
+ street : "times square" ,
190
+ zipcode : "10036"
191
+ }
192
+ } ) ;
193
+ var item2 = new ItemChildModel ( {
194
+ address : {
195
+ street : "bryant park" ,
196
+ zipcode : "10030"
197
+ }
198
+ } ) ;
199
+ var item3 = new ItemChildModel ( {
200
+ address : {
201
+ street : "queens" ,
202
+ zipcode : "1002?"
203
+ }
204
+ } ) ;
205
+ var itemParent = new ItemParentModel ( { items :[ item1 , item2 , item3 ] } ) ;
206
+ itemParent . save ( function ( err ) {
207
+ assert . ifError ( err ) ;
208
+ ItemParentModel . findOneAndUpdate (
209
+ { "_id" : itemParent . _id , "items.item_id" : item1 . item_id } ,
210
+ { "$set" :{ "items.$.address" :{ } } } ,
211
+ { new : true } ,
212
+ function ( err , updatedDoc ) {
213
+ assert . ifError ( err ) ;
214
+ assert . ok ( updatedDoc . items ) ;
215
+ assert . ok ( updatedDoc . items instanceof Array ) ;
216
+ assert . ok ( updatedDoc . items . length , 3 ) ;
217
+ assert . ok ( Utils . isObject ( updatedDoc . items [ 0 ] . address ) ) ;
218
+ assert . ok ( Object . keys ( updatedDoc . items [ 0 ] . address ) . length , 0 ) ;
219
+ done ( ) ;
220
+ }
221
+ ) ;
222
+ } ) ;
223
+ } ) ;
224
+ } ) ;
225
+
160
226
it ( 'returns the original document' , function ( done ) {
161
227
var db = start ( )
162
228
, M = db . model ( modelname , collection )
0 commit comments