【发布时间】:2016-10-18 06:33:34
【问题描述】:
在 mongodb 中使用 mongoose 和 Express
所以现在我有一个框的架构,一个框可以包含对象,也可以包含其他框:
我的盒子架构如下所示:
var box = new Schema({
boxId: ObjectId,
boxContents: [{
contentType: {
type: String,
enum: ["box", "object"]
},
typeId: {
subBox: {
type: Schema.Types.ObjectId,
ref: 'box'
},
subObject: {
type: Schema.Types.ObjectId,
ref: 'object'
}
}
}]
});
我确保将我的“参考”标记为相应的型号名称,所以我认为这不是问题所在。我像这样创建一个新文档:
var box1 = new Box({
contents: [{
contentType: 'object',
typeId: {
subObject: object1._id
}
}]
});
当我使用时:
Control.find({}).populate('contents.typeId')
.exec(function(error, posts) {
console.log(JSON.stringify(posts, null, "\t"));
});
它不会填充 subBox 和 subObject 字段 :( 如果我尝试访问 subBox 或 subObject 字段,它会给我未定义。 我做错了什么?
【问题讨论】:
-
数据库中是否存在
subObject和_id? -
是的,我添加了 subObject 和 subBox,当我查看数据库时,都显示了
-
您能否使用数据库中的示例对象更新您的问题?
标签: node.js mongodb mongoose mongoose-populate mongoose-schema