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" }]