Coding Note

MongoDB Model & Schema - UserModel & Schema 생성 본문

Node JS

MongoDB Model & Schema - UserModel & Schema 생성

jinnkim 2022. 2. 28. 12:30

 

인프런, 노드 리액트 기초 강의 #4번 내용 정리

 

MongoDB Model & Schema

 


 

Model

- Schema를 감싸는 역할

 

Schema

- 정보를 지정하는 코드

- Oracle 테이블과 비슷한 역할

 

 

 

 

1. User Model 생성하기

 - 컬럼 (이름, 이메일, 비밀번호, 성, 역할에 따라 숫자 부여, 이미지, 토큰)

 

const mongoose = require('mongoose');

const userSchema = mongoose.Schema({
    name: {
        type: String,
        maxlength: 50
    },
    email: {
        type: String,
        //trip: 공백 제거해주는 역할
        trim: true,
        //유일성
        unique: 1
    },
    password: {
        type: String,
        maxlength: 5
    },
    lastname: {
        type: String,
        maxlength: 50
    },
    rol: {
        type: Number,
        //rol를 지정하지 않을 경우 기본 값 0
        default: 0
    },
    image: String,
    token: {
        type: String
    },
    tokenExp: {
        //token 유효 기간
        type: Number
    }
})

const User = mongoose.model('User', userSchema)
//다른 곳에서도 사용할 수 있도록 설정
module.exports = {}

 

 

코드 정리하기 >

1. const mongoose = require('mongoose');

- mongoose 모듈을 가져온다.

 

2. Mongoose를 이용해 Schema를 생성한다.

3. Schema 태그 안에 컬럼을 작성한다.

 

 

 

'Node JS' 카테고리의 다른 글

BodyParser & PostMan 설치 & 회원 가입하기  (0) 2022.02.28
SSH 생성하고 Git 연동하기  (0) 2022.02.28
[MongoDB] 몽고 DB 연결하기  (0) 2022.02.28
Node JS, Express JS 설치하기  (0) 2022.02.27
Comments