Docs 菜单

Docs 主页启动和管理 MongoDBMongoDB Atlas

sqlGetSchema

在此页面上

  • 语法
  • 参数
  • 输出
  • 例子

sqlGetSchema命令检索为指定集合或视图存储的模式。

db.runCommand({
sqlGetSchema: "<collection-name>|<view-name>"
})
范围
类型
说明
必要性
<collection-name>
字符串
要检索其模式的集合的名称。集合名称或视图名称是必填项。
可选的
<view-name>
字符串
要检索其模式的视图的名称。视图名称或集合名称是必填项。
可选的

如果集合或视图没有模式,该命令将返回以下输出。

{ "ok" : 1, "metadata" : { }, "schema" : { } }

如果集合或视图具有模式,该命令将返回类似于以下的输出。

{
"ok": 1,
"metadata": {
"description": "<description>"
},
"schema": {
"version": NumberLong(1),
"jsonSchema": {}
}
}

metadata.description字段描述了如何为集合设置模式。值可以是以下之一:

generated automatically by Atlas Data Federation

表示模式由 Atlas Data Federation 自动生成。

set using sqlGenerateSchema with setSchemas = true

表示该模式由sqlGenerateSchema命令设置,因为setSchema选项设置为true

set using sqlSetSchema

表示该模式是使用sqlSetSchema命令设置的。

schema 文档包含以下字段:

范围
类型
说明
schema.version
整型
模式的格式版本。值始终为 1。
schema.jsonSchema
文档

集合或视图的JSON schema。 JSON schema 可以包含以下 字段

  • bsonType

  • properties

  • items

要了解有关这些字段的更多信息,请参阅JSON schema 关键字。

考虑名为 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}

以下命令检索为egData集合存储的模式:

db.runCommand({
sqlGetSchema: "egData"
})

上一个命令会返回以下输出。有关输出中字段的更多信息,请参阅输出。

{
"ok" : 1,
"metadata" : {
"description" : "set using sqlGenerateSchema with setSchemas = true"
},
"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"
]
}
}
}
}
}
← sqlSetSchema
教程 →