뭐든 즐기면서 ;)

MongoDB createCollection validator 본문

DB(데이타베이스)/MongoDB

MongoDB createCollection validator

Tada.*+ 2023. 5. 11. 20:19
728x90

아래와 같이 2번째 인자 없이 collection명만 작성 후 생성하면, validator가 적용되지 않아 field의 타입 제약이 없다.

db.createCollection('user');

반면에, 아래와 같이 2번째 인자를 넣고 validator 옵션과, validationLevel 옵션을 함께 사용하여 field의 타입을 강제할 수 있다.

db.createCollection('user',
    {
        validator: {
            $jsonSchema: {
                bsonType: 'object',
                title: 'user',
                required: ['client_id', 'user_id', 'password'],
                properties: {
                    client_id: {
                        bsonType: 'string'
                    },
                    user_id: {
                        bsonType: 'string'
                    },
                    password: {
                        bsonType: 'object'
                    }
                }
            }
        }
        // validationLevel 옵션 값은 strict, off, moderate(존재하는 document에만 적용) 3가지이며, default는 strict입니다.
        // validator를 원치 않을 경우 off를 지정해줍니다.
        ,validationLevel : 'strict'
    });

 

728x90

'DB(데이타베이스) > MongoDB' 카테고리의 다른 글

MongoDB $elemMatch  (3) 2023.05.30
MongoDB 조회  (0) 2023.05.13
MongoDB findOneAndUpdate vs updateOne  (0) 2023.05.09
MongoDB arrayFilters  (0) 2023.05.04
MongoDB array push $ne / $addToSet  (2) 2023.04.30
Comments