node.js - Mongoose: Is it possible to set strict:false after defining the schema, like through a middleware or a plugin? -


when define schema in mongoose have option set {strict:false}

is there option have set after you've defined schema? through mongoose middleware or mongoose plugin?

i want create plugin need store additional data database, wouldn't able unless plugin user has either set {strict:false} or added fields want store data in schema themselves, both of unlikely. wondering if there's way me make happen plugin code itself?

mongoose odm, , {strict: true} option in regard applies queries run through mongoose, it's not enforced in mongodb side, documents remain schemaless.

but don't have use mongoose make of queries, can use native mongodb driver underneath in order bypass of that. mongoose gives shortcut in form of model.collection. here's example:

var mongoose = require('mongoose'); var schema = mongoose.schema;  var personschema = new schema({     name: string }, {     strict: true });  var person = mongoose.model('person', personschema);  mongoose.connect('mongodb://localhost/test', function(err) {     if (err) {         throw err;     }      person.collection.insert({         name: 'bill',         extraprop: 'hello world'     }, function(err, result) {             if (err) {                 throw err;             }             console.log(result);         });     console.log('connected'); }); 

edit:

you can disable strict specific model paths. think more you're looking for.

personschema.pre('save', function(next) {     this.set('extraprop', 'hello', {         strict: false     });     next(); }); 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -