Node.js Mongoose-子文档的导入架构
在我的Nodejs服务器中,我使用的是mongoose 我在一个文件中为帖子和评论创建了模式。 有没有办法将子文档(注释)的模式导出到单独的文件中,并导入到主模式文件中Node.js Mongoose-子文档的导入架构,node.js,mongodb,mongoose,mongoose-schema,Node.js,Mongodb,Mongoose,Mongoose Schema,在我的Nodejs服务器中,我使用的是mongoose 我在一个文件中为帖子和评论创建了模式。 有没有办法将子文档(注释)的模式导出到单独的文件中,并导入到主模式文件中 const mongoose = require('mongoose'); const commentSchema = mongoose.Schema({ body: { type: String } }); const postSchema = mongoose.Schema({ title: {
const mongoose = require('mongoose');
const commentSchema = mongoose.Schema({
body: { type: String }
});
const postSchema = mongoose.Schema({
title: {
type: String,
required: true
},
comments: {
type: [commentSchema]
}
});
module.exports = mongoose.model('questions', postSchema);
真的不清楚你在问什么。请举例说明您预期会发生什么。您可以在
fileA
上声明子架构,并将其导入到fileB
和fileC
中使用。也可以从文件中导出模型和子架构。