Create a dynamic circuit using mongoose

I am very new to Node js and Mongoose module. I am trying to create a schema where there are some required fields, and some others may be dynamic.

I used strictto false. My code is as follows:

var mongoose = require('mongoose')
var db = mongoose.connect('mongodb://localhost/ets',function(err)
{
    if(err) throw err
})

var Schema = mongoose.Schema
var Tasks = new Schema({vmProfile:String}, { strict: false });
mongoose.model('Task',Tasks)

var Task = mongoose.model('Task')
var task = new Task()
task.vmProfile = "required value"
task.otherKey = "something"
task.save(function(err)
{
    if(err) throw err;
})

when I run this, I get only vmProfile, and not otherKey, the DB looks like this:

{ "vmProfile" : "required value", "_id" : ObjectId("53364a5a5cd71a76122f0a8a"), "__v" : 0 }

where am i making a mistake.

+4
source share
1 answer

From Mongoose docs :

NOTE. Any set of keys / rollers in an instance that does not exist in your schema is always ignored, regardless of the schema.

:

var task = new Task({'otherKey', 'some value'});

mixed -.

+1

Source: https://habr.com/ru/post/1533996/


All Articles