mongoose schema options

mangojang·2021년 12월 28일
0
post-thumbnail

본 글은 글쓴이가 꺼내보기 위함의 목적이 큰 글입니다 :)

1. unique

  • 단 한개의 값만 존재 할 수 있음.
  • find 로 검색해서 찾을때 좀 더 빠르게 찾을 수 있게 해줌.
    username:{
    	type: String
    	,trim: true // 공백제거
    	,unique:true
    }

2. index

  • 보조색인
  • find 로 검색해서 찾을때 좀 더 빠르게 찾을 수 있게 해줌.
  • find로 검색 할 조건으로 쓰이는것에 index 옵션 주면 성능 향상.
    email:{
    	type: String,
    	index: true
    }

3. require

  • 존재유무 검색하고 없다면 데이터 저장 안함.
    username:{
    	type: String
    	,trim: true // 공백제거
    	,unique:true
    	,required: true
    }

4. match

  • 형식에 관련된 검증 후, 정의한 패턴이 아닐 경우 데이터를 저장 안함.
  • 정규표현식 사용
    email:{
    	type: String,
    	index: true,
    	match: /.+\@.+@..+/
    }

5. validate

  • 검증 함수 사용, 검증 통과 못하면 저장 안함.
    password:{
    	type: String,
    	validate:[
    		function(password){
    			return password.length >= 8;
    		}
    		'Password should be longer'
    	]
    }

참고문헌

https://alexband.tistory.com/24?category=664373
https://poiemaweb.com/mongoose

profile
한 걸음 한 걸음 계속 걷는 자가 일류다

0개의 댓글