I have the following schema
var Schema = new mongoose.Schema({
type: {required: true, type: String, enum: ["device", "beacon"], index: true},
device: {
type: {type: String},
version: {type: String},
model: {type: String}
},
name: String,
beaconId: {required: false, type: mongoose.Schema.Types.ObjectId},
lastMeasuredTimestamp: {type: Number, index: true},
lastMeasuredPosition: {type: [Number], index: "2dsphere"},
lastMeasuredFloor: {type: Number, index: true}
}, {strict: false});
Note that I have set strict to false. This is because it is valid to add custom properties not defined in the schema to the document.
Next I do the following query
DB.Document.update({_id: "SOME_ID_HERE"}, {$set: {type: "bull"}}, {runValidators: true})
This will change the property 'type' to a value which is invalid according to the Mongoose schema. I use the runValidators option to ensure that the schema validation is run.
The end result of this query however is that 'type' is changed to 'bull' and no validation is run. When I set strict to true however the validation does run and an error is (correctly) shown.
Why does strict influence whether or no the validation runs? When I look at this description http://mongoosejs.com/docs/guide.html#strict it only mentions that strict limits adding properties not defined in the schema (something I do not want for this specific schema).
Install info:
- Ubuntu 14.04 LTS
- MongoDB 3.0.8
- Mongoose 4.2.3
- NodeJS 0.10.25
- NPM 1.3.10