@@ -4125,6 +4125,55 @@ describe('Model', function() {
4125
4125
assert . equal ( err . validationErrors [ 0 ] . errors [ 'num' ] . name , 'CastError' ) ;
4126
4126
} ) ;
4127
4127
4128
+ it ( 'handles array filters (gh-14978)' , async function ( ) {
4129
+ const embedDiscriminatorSchema = new mongoose . Schema ( {
4130
+ field1 : String
4131
+ } ) ;
4132
+
4133
+ const embedSchema = new mongoose . Schema ( {
4134
+ field : String ,
4135
+ key : String
4136
+ } , { discriminatorKey : 'key' } ) ;
4137
+ embedSchema . discriminator ( 'Type1' , embedDiscriminatorSchema ) ;
4138
+
4139
+ const testSchema = new mongoose . Schema ( {
4140
+ testArray : [ embedSchema ]
4141
+ } ) ;
4142
+ const TestModel = db . model ( 'Test' , testSchema ) ;
4143
+
4144
+ const test = new TestModel ( {
4145
+ testArray : [ {
4146
+ key : 'Type1' ,
4147
+ field : 'field' ,
4148
+ field1 : 'field1'
4149
+ } ]
4150
+ } ) ;
4151
+ const r1 = await test . save ( ) ;
4152
+ assert . equal ( r1 . testArray [ 0 ] . field1 , 'field1' ) ;
4153
+
4154
+ const field1update = 'field1 update' ;
4155
+ await TestModel . bulkWrite ( [ {
4156
+ updateOne : {
4157
+ filter : { _id : r1 . _id } ,
4158
+ update : {
4159
+ $set : {
4160
+ 'testArray.$[element].field1' : field1update ,
4161
+ 'testArray.$[element].nonexistentProp' : field1update
4162
+ }
4163
+ } ,
4164
+ arrayFilters : [
4165
+ {
4166
+ 'element._id' : r1 . testArray [ 0 ] . _id ,
4167
+ 'element.key' : 'Type1'
4168
+ }
4169
+ ]
4170
+ }
4171
+ } ] ) ;
4172
+ const r2 = await TestModel . findById ( r1 . _id ) . lean ( ) ;
4173
+ assert . equal ( r2 . testArray [ 0 ] . field1 , field1update ) ;
4174
+ assert . strictEqual ( r2 . testArray [ 0 ] . nonexistentProp , undefined ) ;
4175
+ } ) ;
4176
+
4128
4177
it ( 'with child timestamps and array filters (gh-7032)' , async function ( ) {
4129
4178
const childSchema = new Schema ( { name : String } , { timestamps : true } ) ;
4130
4179
@@ -8147,6 +8196,52 @@ describe('Model', function() {
8147
8196
assert . ok ( obj . post . updatedAt . valueOf ( ) , new Date ( '2023-06-01T18:00:00.000Z' ) . valueOf ( ) ) ;
8148
8197
} ) ;
8149
8198
} ) ;
8199
+
8200
+ describe ( 'diffIndexes()' , function ( ) {
8201
+ it ( 'avoids trying to drop timeseries collections (gh-14984)' , async function ( ) {
8202
+ const version = await start . mongodVersion ( ) ;
8203
+ if ( version [ 0 ] < 5 ) {
8204
+ this . skip ( ) ;
8205
+ return ;
8206
+ }
8207
+
8208
+ const schema = new mongoose . Schema (
8209
+ {
8210
+ time : {
8211
+ type : Date
8212
+ } ,
8213
+ deviceId : {
8214
+ type : String
8215
+ }
8216
+ } ,
8217
+ {
8218
+ timeseries : {
8219
+ timeField : 'time' ,
8220
+ metaField : 'deviceId' ,
8221
+ granularity : 'seconds'
8222
+ } ,
8223
+ autoCreate : false
8224
+ }
8225
+ ) ;
8226
+
8227
+ const TestModel = db . model (
8228
+ 'TimeSeriesTest' ,
8229
+ schema ,
8230
+ 'gh14984'
8231
+ ) ;
8232
+
8233
+ await db . dropCollection ( 'gh14984' ) . catch ( err => {
8234
+ if ( err . codeName === 'NamespaceNotFound' ) {
8235
+ return ;
8236
+ }
8237
+ throw err ;
8238
+ } ) ;
8239
+ await TestModel . createCollection ( ) ;
8240
+
8241
+ const { toDrop } = await TestModel . diffIndexes ( ) ;
8242
+ assert . deepStrictEqual ( toDrop , [ ] ) ;
8243
+ } ) ;
8244
+ } ) ;
8150
8245
} ) ;
8151
8246
8152
8247
0 commit comments