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
Post a Comment