Say I have two mongoose schemas:
var AccountSchema = new Schema({
userName: String
, password: String
})
var AgentSchema = new Schema({
Name : String
, Account: AccountSchema
})
is there anyway to add AccountSchema to the AgentSchema without it being a collection?
It doesn't look like it's possible. The two solutions are to either use a 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(); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With