Is it possible to have nested schemas in mongoose and have a required validator on the children? Something like this:
是否可以在mongoose中使用嵌套模式并在子项上具有必需的验证器?像这样的东西:
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true
}
});
const eventSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
host: {
type: userSchema,
required: true
}
});
I can't find anything in the documentation. Thanks.
我在文档中找不到任何内容。谢谢。
3 个解决方案
#1
4
Yes, your schema is correct.
是的,您的架构是正确的。
The docs for mongoose nested schema (SubDocuments) can be found here
可以在此处找到mongoose嵌套模式(SubDocuments)的文档
#2
#3
0
i suppose you'll update eventSchema with subdocuments of type user model. you can use { runValidators: true}
for update.
我想你将使用用户模型类型的子文档更新eventSchema。您可以使用{runValidators:true}进行更新。
eventModel.update({ name: 'YOUR NAME' }, { $push: { host: user } }, { runValidators: true}, function(err) {
})
#1
4
Yes, your schema is correct.
是的,您的架构是正确的。
The docs for mongoose nested schema (SubDocuments) can be found here
可以在此处找到mongoose嵌套模式(SubDocuments)的文档
#2
0
You can use the nested schema in mongoose.
您可以在mongoose中使用嵌套模式。
It will also give you he Object Id on each sub schema values as well.
它还会在每个子模式值上为您提供Object Id。
#3
0
i suppose you'll update eventSchema with subdocuments of type user model. you can use { runValidators: true}
for update.
我想你将使用用户模型类型的子文档更新eventSchema。您可以使用{runValidators:true}进行更新。
eventModel.update({ name: 'YOUR NAME' }, { $push: { host: user } }, { runValidators: true}, function(err) {
})