node.js - 猫鼬中的链接模型
问题描述
我是 mongoose 和 nosql 数据库的新手。我有以下猫鼬模型(配置文件)。
import { model, Schema } from 'mongoose';
import Joi from '@hapi/joi';
const profileSchema = new Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'users',
},
handle: {
type: String,
minlength: 2,
maxlength: 20,
required: true,
trim: true,
},
company: {
type: String,
minlength: 1,
maxlength: 100,
trim: true,
},
website: {
type: String,
maxlength: 100,
trim: true,
},
location: {
type: String,
maxlength: 100,
trim: true,
},
status: {
type: String,
maxlength: 50,
trim: true,
required: true,
},
skills: {
type: [String],
required: true,
},
bio: {
type: String,
maxlength: 500,
trim: true,
},
githubUserName: {
type: String,
maxlength: 50,
trim: true,
},
socialLinks: {
youtube: {
type: String,
maxlength: 100,
trim: true,
},
twitter: {
type: String,
maxlength: 100,
trim: true,
},
facebook: {
type: String,
maxlength: 100,
trim: true,
},
linkedin: {
type: String,
maxlength: 100,
trim: true,
},
instagram: {
type: String,
maxlength: 100,
trim: true,
},
},
date: {
type: Date,
default: Date.now,
},
});
export default model('profile', profileSchema);
export const validateProfile = (profile) => {
const schema = Joi.object({
handle: Joi.string().trim().min(2).max(20).required(),
company: Joi.string().trim().min(2).max(20),
website: Joi.string().trim().max(100),
location: Joi.string().trim().min(2).max(20),
status: Joi.string().trim().min(2).max(20),
skills: Joi.array().required(),
bio: Joi.string().trim().max(500),
githubUserName: Joi.string().max(50),
youtube: Joi.string().trim().max(100),
twitter: Joi.string().trim().max(100),
facebook: Joi.string().trim().max(100),
linkedin: Joi.string().trim().max(100),
instagram: Joi.string().trim().max(100),
});
return schema.validate(profile);
};
每个配置文件都可以有多种体验。这是我的经验模型。
import { model, Schema } from 'mongoose';
import Joi from '@hapi/joi';
const experienceSchema = new Schema({
title: {
type: String,
maxlength: 100,
trim: true,
required: true,
},
company: {
type: String,
maxlength: 100,
trim: true,
required: true,
},
location: {
type: String,
maxlength: 100,
trim: true,
required: true,
},
from: {
type: Date,
required: true,
},
to: {
type: Date,
},
current: {
type: Boolean,
default: false,
},
description: {
type: String,
maxlength: 500,
trim: true,
},
});
export default model('experience', experienceSchema);
export const validateExperience = (profile) => {
const schema = Joi.object({
title: Joi.string().trim().max(100).required(),
company: Joi.string().trim().max(100).required(),
location: Joi.string().trim().max(100).required(),
from: Joi.date().required(),
to: Joi.date(),
current: Joi.boolean().default(false),
description: Joi.string().trim().max(500),
});
return schema.validate(profile);
}
;
我想将此体验模型链接到配置文件模型。经验是一个数组。那么如何将这个体验模型数组链接到配置文件模型呢?首先,我想将体验数组保留在配置文件模型中,但这会使模型太大。我想在这里分开一些东西。这里应该有什么更好的做法?
解决方案
您可以在 Profile Schema 中添加一个字段experience_ids
,它将在 Profile Schema 中保存经验 ID
experience_ids: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'experience'
}]
您可以像这样填充经验:
Profile
.find()
.populate('experience_ids')
.exec(...)
推荐阅读
- python - 替换列表中的最小值和最大值
- spring-boot - Spring Boot REST 路径,例如配置文件和用户
- azure - Azure Web 应用程序功能未通过 crontab 触发
- html - 在 html 页面中选择国家下拉列表
- html - How can I paste Excel and MS Word table and edit it on website in Angular
- linux - Communication between Windows Host and Linux Docker Container
- c# - 为所有视图控件添加自定义属性
- android - Android webView - 如何将上传文件设置为仅图像?
- reactjs - 更改多个项目中按钮上的文本(React.js)
- ruby-on-rails - Rails link_to 标签不更新 url