0
我得到了這樣的事情:貓鼬文檔不填充
let fooSchema = new mongoose.Schema({
bars: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Bar' }],
});
和
let barSchema = new mongoose.Schema({
sth1: String,
sth2: String,
sth3: String,
});
兩種模式在不同的文件並導出爲貓鼬模型。
所以我有一個foo
文件空bars
陣列和一個bar
文件,例如:
let foo = new Foo({ bars: [] });
let bar = new Bar({ sth1: "1", sth2: "2", sth3: "3" });
後來,當我推bar
到foo
小號bars
和控制檯日誌這陣我:
foo.bars.push(bar);
console.log(foo.bars);
//it outputs:
["59760dcbe3a7e31c2693ce47"]
所以foo.bars
只有ID。 我應該怎麼做才能在這個數組中有整個文檔(沒有保存,然後找到並填充這個字段)?
我想實現的是:
foo.bars.push(bar);
console.log(foo.bars);
[{ _id: 59760dcbe3a7e31c2693ce47, sth1: "1", sth2: "2", sth3: "3" }]