스키마 만들기
이 페이지의 내용
Atlas UI를 사용하여 스키마 만들기
스키마 생성은 Atlas SQL 빠른 시작 연결을 만들 때 자동으로 발생합니다.
Atlas CLI를 사용하여 스키마 생성
sqlGenerateSchema
명령은 지정된 컬렉션 또는 뷰에 대한 Atlas SQL 스키마를 생성합니다.
구문
db.runCommand({ sqlGenerateSchema: 1, sampleNamespaces: [<namespace>], sampleSize: <int>, setSchemas: true|false })
매개변수
매개변수 | 유형 | 설명 | 필요성 |
---|---|---|---|
sampleNamespaces | 문자열 배열 | Specifies the comma-separated list of namespaces for which to generate schemas. A namespace includes the database name, a dot (.) separator, and the collection or view name (i.e. <database>.<collection>|<view> ). To generate schemas for all the collections in a database, specify * instead of the collection or view name (i.e. <database>.* ). If omitted, generates schemas for all collections and views in the current database. | 옵션 |
sampleSize | integer | 스키마를 생성하기 위해 샘플로 사용할 문서 수를 지정합니다. 생략하는 경우 기본값은 1000 입니다. | 옵션 |
setSchemas | 부울 | 컬렉션 또는 뷰에 대해 생성된 스키마를 저장할지 여부를 지정합니다. 값은 다음 중 하나일 수 있습니다.
생략하는 경우 기본값은 | 옵션 |
출력
명령이 성공하면 이 명령은 다음을 반환합니다.
{ "ok" : 1, "schemas" : [ { "databaseName" : "<database-name>", "namespaces" : [ { "name" : "<collection-name>", "schema" : { "version" : NumberLong(1), "jsonSchema" : {} } } ] }, ... ] }
schemas
객체에 다음 필드가 포함되어 있습니다.
매개변수 | 유형 | 설명 |
---|---|---|
databaseName | 문자열 | 데이터베이스의 이름입니다. |
namespaces | 객체 배열 | 각 컬렉션 또는 뷰의 이름 및 생성된 스키마입니다. |
namespaces.name | 문자열 | 컬렉션 또는 뷰의 이름입니다. |
namespaces[n].schema | 문서 | 컬렉션 또는 뷰의 스키마입니다. |
namespaces[n].schema.version | integer | 스키마의 형식 버전입니다. 값은 항상 1입니다. |
namespaces[n].schema.jsonSchema | 문서 | JSON schema of the collection or view. The JSON schema can contain the following fields:
To learn more about these fields, see JSON Schema Keywords. |
setSchemas
옵션을 사용하여 컬렉션 또는 뷰에 대한 스키마를 설정하는 경우 sqlGetSchema
명령을 실행하여 명령이 성공했는지 확인할 수 있습니다. sqlGetSchema
명령 metadata.description
에는 다음 값이 포함되어야 합니다.
"set using sqlGenerateSchema with setSchemas = true"
예제
다음 문서가 포함된 sampleDB
라는 데이터베이스에 있는 egData
컬렉션을 고려해 보세요.
{"a": {"b": {"c": [1, 2, 3]}}, "s": 1} {"a": {"b": {"c": [4, 5, 6]}}, "s": 2} {"a": {"b": [7, 8, 9]}, "s": 3} {"a": {"b": {"c": []}}, "s": 4} {"a": {"b": {"c": "hello"}}, "s": 5} {"a": {"b": {"c": {"d": 1}}}, "s": 6} {"a": {"b": {"c": null}}} {"s": 7}
아래 예시에서는 sqlGenerateSchema
명령을 사용하여 위 컬렉션에 대한 스키마를 생성합니다.
기본 예시
다음 명령은 스토리지 구성에서 sampleDB.egData
라는 컬렉션에 대한 스키마를 생성합니다. sampleSize
가 2
이기 때문에 명령은 컬렉션에서 무작위로 선택된 두 개의 문서를 사용하여 스키마를 생성합니다. setSchemas
옵션이 명령과 함께 지정되지 않고 기본값이 false
이므로 이 명령은 컬렉션에 대한 스키마를 설정하지 않습니다.
db.runCommand({ sqlGenerateSchema: 1, sampleNamespaces: ["sampleDB.egData"], sampleSize: 2 })
이전 명령은 다음 출력을 반환합니다. 출력의 필드에 대해 자세히 알아보려면 출력을 참조하세요 .
{ "ok" : 1, "schemas" : [ { "databaseName" : "sampleDB", "namespaces" : [ { "name" : "egData", "schema" : { "version" : NumberLong(1), "jsonSchema" : { "bsonType" : [ "object" ], "properties" : { "a" : { "bsonType" : [ "object" ], "properties" : { "b" : { "bsonType" : [ "object" ], "properties" : { "c" : { "bsonType" : [ "array" ], "items" : { "bsonType" : [ "int" ] } } } } } }, "s" : { "bsonType" : [ "int" ] } } } } } ] } ] }
스키마 예시 생성 및 설정
다음 명령은 스토리지 구성에서 sampleDB.egData
라는 컬렉션에 대한 스키마를 생성합니다. sampleSize
옵션이 명령과 함께 지정되지 않고 기본값이 1000
이므로 이 명령은 컬렉션에서 최대 1000개의 문서를 사용하여 스키마를 생성합니다. 이 명령은 setSchemas
옵션이 true
로 설정되어 있기 때문에 생성된 스키마를 컬렉션에 사용할 스키마로 설정합니다.
db.runCommand({ sqlGenerateSchema: 1, sampleNamespaces: ["sampleDB.egData"], setSchemas: true })
이전 명령은 다음 출력을 반환합니다. 출력의 필드에 대해 자세히 알아보려면 출력을 참조하세요 .
{ "ok" : 1, "schemas" : [ { "databaseName" : "sampleDB", "namespaces" : [ { "name" : "egData", "schema" : { "version" : NumberLong(1), "jsonSchema" : { "bsonType" : [ "object" ], "properties" : { "a" : { "bsonType" : [ "object" ], "properties" : { "b" : { "bsonType" : [ "object", "array" ], "properties" : { "c" : { "bsonType" : [ "array", "string", "object", "null" ], "properties" : { "d" : { "bsonType" : [ "int" ] } }, "items" : { "bsonType" : [ "int" ] } } }, "items" : { "bsonType" : [ "int" ] } } } }, "s" : { "bsonType" : [ "int", "object" ] } } } } ] } ] }
오류
명령이 실패하면 다음과 같은 오류가 반환됩니다.
"failedNamespaces": [ { "namespace" : "<db.ns>", "error" : "no documents found in sample namespace" } ]
지정된 네임스페이스가 스토리지 구성에 없거나 비어 있는 경우 위의 오류가 반환됩니다. 지정된 네임스페이스에 스키마를 설정할 수 없는 경우에도 이 오류가 반환됩니다.