如何在 mongo 中将自定义集合模型设置为数据类型?
How to set custom collection model as dataType in mongo?
所以我已经使用 ClientShema 创建了模型 Client,现在我正在创建模型 Company。其中一个字段应该是 Client 类型,我试过这样做,但它似乎崩溃了
这是一些代码:
const userSchema = new mongoose.Schema({
email: {
type: String,
},
username: {
type: String,
required: false
},
createdAt: {
type: Date,
required: true
},
_id: {
type: String,
required: false,
}
}, { collection: 'User' });
const User = mongoose.model('User', userSchema);
这是我的文件,我想在其中使用之前提供的模型。
const client = require('./client');
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [client.Client]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
欢迎使用 Whosebug
const client = require('./client');// need to include the schema file
var ClientSchema = mongoose.model('Client').schema;// 'Client' is name of Schema
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [ClientSchema]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
希望这能解决您的问题。
所以我已经使用 ClientShema 创建了模型 Client,现在我正在创建模型 Company。其中一个字段应该是 Client 类型,我试过这样做,但它似乎崩溃了
这是一些代码:
const userSchema = new mongoose.Schema({
email: {
type: String,
},
username: {
type: String,
required: false
},
createdAt: {
type: Date,
required: true
},
_id: {
type: String,
required: false,
}
}, { collection: 'User' });
const User = mongoose.model('User', userSchema);
这是我的文件,我想在其中使用之前提供的模型。
const client = require('./client');
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [client.Client]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
欢迎使用 Whosebug
const client = require('./client');// need to include the schema file
var ClientSchema = mongoose.model('Client').schema;// 'Client' is name of Schema
const companySchema = new mongoose.Schema({
_id: {
type: String,
required: true
},
logo: {
type: String,
required: false
},
companyName: {
type: String
},
clients: {
type: [ClientSchema]
}
}, { collection: 'Company' });
const Company = mongoose.model('Company', companySchema);
希望这能解决您的问题。