假设我有两个mongoose模式:
var AccountSchema = new Schema({
userName: String
, password: String
})
var AgentSchema = new Schema({
Name : String
, Account: AccountSchema
})
无论如何都要将AccountSchema添加到AgentSchema而不是集合吗?
答案 0 :(得分:4)
看起来不太可能。这两种解决方案要么使用DocumentId,要么使用虚拟:
<强>的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(); });