Node.js 猫鼬模型分离
我是node和mongodb的新手。我有以下猫鼬模型Node.js 猫鼬模型分离,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我是node和mongodb的新手。我有以下猫鼬模型 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,
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,
},
experience: [
{
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,
},
},
],
education: [
{
school: {
type: String,
maxlength: 100,
trim: true,
required: true,
},
degree: {
type: String,
maxlength: 100,
trim: true,
required: true,
},
fieldOfStudy: {
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,
},
},
],
social: {
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);
我在一个文件中创建了这个模型,它看起来太大了。那么,我应该把经验、教育和社会分为三个不同的模式吗?如果是这样,我应该怎么做?如果我把它们放在3个独立的模型中,我如何将它们与概要模型链接?举个例子会非常有用。是的,你应该把它们分开。要链接它们,只需将概要模式Id作为字段放在其他模型上
const profileSchema = new Schema({
userId: Schema.Types.ObjectId
})
const experienceSchema = new Schema({
userId: Schema.Types.ObjectId
})
const educationSchema = new Schema({
userId: Schema.Types.ObjectId
})
然后,您只需通过userId
查询体验集合即可获得他们的体验。这是我推荐的方式
另一种方法是将
experienceID
放在参考体验模型的概要模式上,并可以使用populate
方法填充字段。您已经将用户链接到了您的概要模式,因此您也可以对体验、教育等使用相同的方法。您也可以查看子文档方法,谢谢。谢谢你的帮助,谢谢你的回答。