LoginSignup
0
1

More than 5 years have passed since last update.

mongooseでSchema定義してないpropertyに値をセットする

Posted at

mongooseを利用していると通常Schema定義外の値をSchemaのDocumentオブジェクトにセットすることはできないが、 { strict: false } すればできるよう

var somethingSchema = new Schema({
  field: String
});
var Something = mongoose.model('Something', somethingSchema);

var something = new Something();

something.not_defined_field = 'Not defined field value';
something('not_defined_field'); // -> undefined
something.not_defined_field; // -> undefined


something.set('not_defined_field', 'Not defined field value', { strict: false });
something('not_defined_field'); // -> 'Not defined field value'
something.not_defined_field; // -> undefined

でドキュメントみてたらSchemaに { strict: false } すればDocument全体に適用できるっぽい
http://mongoosejs.com/docs/guide.html#strict

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1