collection을 생성한 후 유효성 검사를 추가하거나 기존 유효성 검사 스키마를 수정하는 경우, collection에 유효하지 않은 문서가 있을 수 있습니다. 마찬가지로 스키마의 validationAction
가 warn
인 경우 collection에 유효하지 않은 문서가 포함될 수 있습니다. 유효하지 않은 문서를 쿼리하여 잠재적으로 업데이트하거나 collection에서 삭제할 수 있습니다.
지정된 스키마 와 일치하거나 일치하지 않는 문서를 찾으려면 쿼리 연산자와 함께 $jsonSchema
를 사용합니다. Similarly, you can update or delete documents based on a schema by using $jsonSchema
in query conditions for write operations.
예시
다음 문서를 사용하여 샘플 collection inventory
을 만듭니다.
db.inventory.insertMany( [ { item: "journal", qty: 25, size: { h: 14, w: 21, unit: "cm" }, instock: true }, { item: "notebook", qty: 50, size: { h: 8.5, w: 11, unit: "in" }, instock: true }, { item: "paper", qty: 100, size: { h: 8.5, w: 11, unit: "in" }, instock: 1 }, { item: "apple", qty: 45, status: "A", instock: true }, { item: "pears", qty: 50, status: "A", instock: true } ] )
스키마 객체 정의하기
샘플 스키마 객체를 정의하고 이를 myschema
라는 변수에 저장합니다.
let myschema = { $jsonSchema: { required: [ "item", "qty", "instock" ], properties: { item: { bsonType: "string" }, qty: { bsonType: "int" }, size: { bsonType: "object", required: [ "unit" ], properties: { unit: { bsonType: "string" }, h: { bsonType: "double" }, w: { bsonType: "double" } } }, instock: { bsonType: "bool" } } } }
스키마 다음 유효성 검사 적용합니다.
필수 필드:
item
BSON typestring
이어야 합니다.qty
BSON typeinteger
이어야 합니다.instock
BSON typeboolean
이어야 합니다.
size
, 존재하는 경우:BSON type
object
이어야 합니다.필수
string
필드 로unit
을(를) 포함해야 합니다.포함된
h
및w
필드가 있는 경우double
유형이어야 합니다.
스키마와 일치하는 문서 찾기
이 명령은 스키마와 일치하는 모든 문서를 반환합니다.
db.inventory.find(myschema) db.inventory.aggregate( [ { $match: myschema } ] )
두 명령 모두 동일한 결과를 반환합니다.
[ { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]
스키마와 일치하지 않는 문서 찾기
컬렉션에서 스키마 유효성 검사 규칙과 일치하지 않는 문서를 찾으려면 $nor
연산자와 함께 $jsonSchema
를 사용합니다. 예를 들면 다음과 같습니다.
db.inventory.find( { $nor: [ myschema ] } )
출력:
[ // Neither size.h nor size.w are type double { _id: ObjectId("62b5cd5a14b92d148400f79e"), item: 'journal', qty: 25, size: { h: 14, w: 21, unit: 'cm' }, instock: true }, // size.w is not a double { _id: ObjectId("62b5cd5a14b92d148400f79f"), item: 'notebook', qty: 50, size: { h: 8.5, w: 11, unit: 'in' }, instock: true }, // size.w is not a double and instock is not a boolean { _id: ObjectId("62b5cd5a14b92d148400f7a0"), item: 'paper', qty: 100, size: { h: 8.5, w: 11, unit: 'in' }, instock: 1 } ]
스키마와 일치하지 않는 문서 업데이트
이 명령은 스키마와 일치하지 않는 모든 문서를 업데이트하고 문서의 isValid
필드를 false
로 설정합니다.
db.inventory.updateMany( { $nor: [ myschema ] }, { $set: { isValid: false } } )
컬렉션을 쿼리하여 업데이트를 확인할 수 있습니다.
db.inventory.find()
출력:
[ { _id: ObjectId("62b5cd5a14b92d148400f79e"), item: 'journal', qty: 25, size: { h: 14, w: 21, unit: 'cm' }, instock: true, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f79f"), item: 'notebook', qty: 50, size: { h: 8.5, w: 11, unit: 'in' }, instock: true, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a0"), item: 'paper', qty: 100, size: { h: 8.5, w: 11, unit: 'in' }, instock: 1, isValid: false }, { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]
스키마와 일치하지 않는 문서 삭제하기
이 명령은 스키마와 일치하지 않는 모든 문서를 삭제합니다.
db.inventory.deleteMany( { $nor: [ myschema ] } )
컬렉션을 쿼리하여 업데이트를 확인할 수 있습니다.
db.inventory.find()
출력:
[ { _id: ObjectId("62b5cd5a14b92d148400f7a3"), item: 'apple', qty: 45, status: 'A', instock: true }, { _id: ObjectId("62b5cd5a14b92d148400f7a4"), item: 'pears', qty: 50, status: 'A', instock: true } ]