【发布时间】:2013-11-07 06:50:24
【问题描述】:
我了解如何在 Mongoose 中使用 embed documents,如果存储为数组,它们看起来相当简单,其用例相当明显:
var CommentSchema = new Mongoose.Schema({...});
var BlogPostSchema = new Mongoose.Schema({
comments : [CommentSchema],
});
但是,在前后查看文档后,我不知道如何存储一个不需要或不想放在数组中的子文档。
var UserSchema = new Mongoose.Schema({...});
var BlogPostSchema = new Mongoose.Schema({
author: ??? // 'UserSchema' and UserSchema do not work here.
});
有什么办法可以使这个工作吗?我不想只存储 ObjectId,而是存储用户记录的完整副本,但不需要或不需要数组。
【问题讨论】:
-
您可以从 4.2.0 版开始执行此操作,请参阅 mongoosejs.com/docs/subdocs.html#single-embedded