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

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -