node.js - How do you define a nested object to an existing schema in Mongoose? -


say have 2 mongoose schemas:

var accountschema = new schema({         username: string      , password: string }) var agentschema = new schema({      name : string    , account: accountschema }) 

is there anyway add accountschema agentschema without being collection?

it doesn't it's possible. 2 solutions either use documentid or virtuals:

objectid:

var mongoose = require('mongoose')   , schema = mongoose.schema   , objectid = schema.objectid;  var accountschema = new schema({         username: string      , password: string }) var agentschema = new schema({      name : string    , account: {type: objectid} }) 

virtuals:

var accountschema = new schema({         username: string      , password: string }) var agentschema = new schema({      name : string    , _accounts: [accountschema] })  agentschema.virtual('account')     .set(function(account) { this._accounts[0] = account; })     .get(function() { return this._accounts.first(); });  

Comments

Popular posts from this blog

Python __call__ special method practical example -

How can I edit my VIM config so that Vim treats ".ejs" files the same as it currently treats my html files? -