【发布时间】:2017-07-24 23:53:48
【问题描述】:
我得到了这样的东西:
let fooSchema = new mongoose.Schema({
bars: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Bar' }],
});
和
let barSchema = new mongoose.Schema({
sth1: String,
sth2: String,
sth3: String,
});
两种模式都在单独的文件中,并作为猫鼬模型导出。
所以我有一个带有空bars 数组的foo 文档和一个bar 文档,例如:
let foo = new Foo({ bars: [] });
let bar = new Bar({ sth1: "1", sth2: "2", sth3: "3" });
然后,当我将 bar 推入 foos 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" }]
【问题讨论】:
标签: node.js mongodb mongoose mongoose-schema mongoose-populate