最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

猫鼬模型分离

IT培训 admin 5浏览 0评论

猫鼬模型分离

是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,
    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来查询体验集合以获取他们的体验。这是我推荐的方式。

另一种方法是将experienceIds放在引用体验模型的配置文件架构上,并可以使用populate方法填充字段。

猫鼬模型分离

是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,
    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来查询体验集合以获取他们的体验。这是我推荐的方式。

另一种方法是将experienceIds放在引用体验模型的配置文件架构上,并可以使用populate方法填充字段。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论