我正在使用带有Mongoose的Node.js,并且我正在尝试链接文档,以便在创建和保存文档时,始终创建参考文档并随其保存:
var ClientSchema = new Schema({
name: {
type: String,
default: ''
},
address: {
type: Schema.ObjectId,
ref: 'Address'
}
});
mongoose.model('Client', ClientSchema);
var AddressSchema = new Schema({
street: {
type: String,
default: ''
},
postcode: {
type: String,
default: ''
}
});
mongoose.model('Address', AddressSchema);
我知道有前后挂钩等,但我仍在努力实际实施。有什么想法吗?