Post

[MongoDB] Schema 스키마 타입 제외 속성들

mongoose 스키마의 다양한 속성

NameDescription
required꼭 입력해야 한다.
unique다른 행과 중복되면 안 된다.
trim공백을 제거합니다.(문자열 타입에 사용)
default문서가 생성되면 기본값으로 저장됩니다.
lowercase대문자를 소문자로 저장한다(문자열 타입)
match정규식으로 저장하려는 값과 비교한다.
validate함수로 개발자가 조건을 만듭니다.
set값을 입력할 때 함수로 조건을 만듭니다.
get값을 출력할 때 함수로 조건을 만듭니다.
ref해당하는 모델을 참조할 때 사용한다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const exampleSchema = mongoose.Schema({
   firstName: {
    type: String,
    required: true,
    trim: true,
    lowercase: true
   },
   lastName:{
    type: String,
    trim: true,
    lowercase: true
   }
   age:{
    type: Number,
    required: true,
   }
   created:{
    type: Date,
    default: Date.now
   },
   number:{
    type: Number,
    match: /^\d{3}-\d{3,4}-\d{4}$/
   },
   password:{
    type: String,
    validate: [
      function(password) {
       return password && password.length>6;
      }, '비밀번호를 입력하거나 길이가 6보다 커야합니다.'
     ]
   },
   author:{
    type: Schema.Types.Id,
    ref: 'User'
   }
});


web_server node mongoose 스키마 타입 속성 메서드

This post is licensed under CC BY 4.0 by the author.