#4 MongoDB Model & Schema

2022. 8. 3. 13:54TIL/따라하며 배우는 노드 리액트

728x90
[인프런] 따라하며 배우는 노드, 리액트 시리즈 - 기본강의를 들으며 정리한 내용입니다.

 

 

 

Model : Schema를 감싸주는 역할

Schema: 상품에 비유했을 때 상품의 이름, 가격, 정보 등을 작성하는 것을 의미한다.

  1. models 폴더 생성하기
  2. 폴더 내부에 User.js 파일 생성하기
  3. User.js 파일 내부에 코드 작성하기
const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
	name: {
		type: String,
		maxlength: 50,
	},
	email: {
		type: String,
		trim: true, //문자열의 공백을 제거해주는 역할
		unique: 1
	},
	password: {
		type: String,
		minlength: 5,
	},
	lastname: {
		type: String,
		manlength: 50,
	},
	role: {
		type: Number,
		default: 0
	},
	image: String,
	token: {
		type: String
	},
	tokenExp: {
		type: Number
	}
})

const User = mongoose.model('User', userSchema)

module.exports = {User}