mongoose需要一个子文件吗?

是否有可能在mongoose嵌套模式,并有一个必要的validation儿童? 像这样的东西:

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 } }); 

我在文档中找不到任何东西。 谢谢。

是的,你的模式是正确的。

可以在这里findmongoose嵌套模式文档(SubDocuments)

你可以在mongoose中使用嵌套的模式。

它也会给你他对每个子模式值的对象ID。

  • Doc: 这里
  • 例如: 在这里