컬렉션의 유효성 검사 규칙을 확인하여 문서에 부과되는 제한 사항과 유효하지 않은 문서가 발생했을 때 MongoDB가 처리하는 방법을 확인할 수 있습니다.
컬렉션의 유효성 검사 규칙을 보려면 db.getCollectionInfos() 메서드 또는 listCollections 데이터베이스 명령 을 사용합니다.
두 명령 모두 동일한 정보를 반환하지만 출력 형식은 각 명령마다 다릅니다.
전제 조건
이 페이지의 예시를 실행하려면 유효성 검사 규칙이 있는 students 컬렉션을 만듭니다. 자세한 내용은 JSON Schema 유효성 검사 지정을 참조하세요.
예시: db.getCollectionInfos() 구문
다음 명령은 db.getCollectionInfos()를 사용하여 students 컬렉션에 대한 유효성 검사 규칙을 반환합니다.
db.getCollectionInfos( { name: "students" } )[0].options.validator 
출력은 다음 유효성 검사 객체와 유사합니다.
{   '$jsonSchema': {     bsonType: 'object',     required: [ 'name', 'year', 'major', 'address' ],     properties: {       name: {         bsonType: 'string',         description: 'must be a string and is required'       },       year: {         bsonType: 'int',         minimum: 2017,         maximum: 3017,         description: 'must be an integer in [ 2017, 3017 ] and is required'       },       gpa: {         bsonType: [ 'double' ],         description: 'must be a double if the field exists'       }     }   } } 
참고
기본적으로 포함되지 않는 유효성 검사 작업 및 수준
validationAction 및 validationLevel이 명시적으로 설정되지 않은 경우 는db.getCollectionInfos() 해당 필드를 출력에 포함하지 않습니다.
예시: listCollections 구문
다음 명령은 listCollections를 사용하여 students 컬렉션에 대한 유효성 검사 규칙을 반환합니다.
db.runCommand ( { listCollections: 1, filter: { name: "students" } } ) 
출력은 다음 객체와 유사합니다.
{   cursor: {     id: Long("0"),     ns: 'test.$cmd.listCollections',     firstBatch: [       {         name: 'students',         type: 'collection',         options: {           validator: {             '$jsonSchema': {               bsonType: 'object',               required: [ 'name', 'year', 'major', 'address' ],               properties: {                 name: {                   bsonType: 'string',                   description: 'must be a string and is required'                 },                 gpa: {                   bsonType: [ 'double' ],                   description: 'must be a double if the field exists'                 }               }             },             validationAction: 'warn'           }         },         info: {           readOnly: false,           uuid: UUID("bf560865-5879-4ec1-b389-f77a03abbc5a")         },         idIndex: { v: 2, key: { _id: 1 }, name: '_id_' }       }     ]   },   ok: 1 }