React basic (2) Mongodb 설치 및 연결, Model과Schema

Jiwontwopunch·2022년 2월 2일
0

스터디

목록 보기
9/16
post-thumbnail

mongodb 설치 및 앱과 연결

mongodb 설치 참고 블로그

몽구스 설치

npm install mongoose --save

Mongodb Model & Schema

Model : schema를 감싸주는 역할
Schema : 하나하나의 정보를 지정하는 역할

예제 실습

const mongoose = require('mongoose');

// schema 생성
const userSchema = mongoose.Schema({
    name :{
        type: String,
        maxlength: 50
    },
    email :{
        type: String,
        trim: true, // 여백공간 없애주는 역할
        unique: 1
    },
    password :{
        type: String,
        minlength: 5
    },
    lastname :{
        type: String,
        maxlength: 50
    },
    role :{
        type: Number,
        default: 0
    },
    image: String,
    token :{ // 유효성 검사
        type: String
    },
    tokenExp :{ // 유효기간
        type: Number
    },
})

// 스키마를 모델로 감싸준다
const User = mongoose.model('User', userSchema)

module.exports = { User }

0개의 댓글