我有一个 Mongoose 模式,其中有 4 个子模式。我一直在关注这里的文档https://github.com/LearnBoost/mongoose关于嵌入文档
var scenarios = new Schema({
title: 'String',
type: 'String',
description: 'String',
authorId: 'String',
categories: [categoriesSchema],
subcategories: [subcategories_schema],
presentation: [presentations_schema],
scripts: [scripts_schema],
revision: 'String',
createDate: 'String',
updateDate: 'Date',
active: 'Boolean',
display: 'Boolean',
status: [statusSchema],
video: [video_schema],
bundleId: [bundleSchema],
sortOrder: 'Number'
});
我的问题是,例如,如果我不希望演示文稿成为数组,并且只想每个场景允许 1 个演示文稿,是否有办法在架构定义上处理该问题?
最佳答案
如果不声明数组,您就无法嵌入架构对象,但是您可以将演示架构定义为纯 JavaScript 对象(而不是架构实例):
var presentations_schema = {
your_field: {type: String, required: true} // etc
}
然后你可以这样做:
presentation: presentations_schema
在 http://mongoosejs.com/docs/guide.html ,请参阅第一个示例中的元字段。
关于mongoose - 如何在 Mongoose 模式中创建不是数组的子模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892750/