在貓鼬中,是否可以在保存被引用文檔時創建引用文檔?我已經嘗試了下面,但它似乎並沒有爲我工作。貓鼬 - 在模型保存中創建參考
var Model1Schema = new Schema({
foo: String,
child: { ref: 'Model2', type: ObjectId }
});
var Model2Schema = new Schema({
foo: String
});
mongoose.model('Model1', Model1Schema);
mongoose.model('Model2', Model2Schema);
var m = new (mongoose.model('Model1'));
m.set({
foo: 'abc',
child: {
bar: 'cba'
}
}).save();
嘿@mike其2017年。你知道現在有一些更好的方法來做到這一點嗎?你也有一個錯誤在m.set孩子應該是:{foo:'cba'} –