Docs 菜单
Docs 主页
/
MongoDB Atlas
/ / /

编辑模式

在此页面上

  • 使用 Atlas 用户界面编辑模式
  • 在 Atlas 中,转到项目的 Clusters页面。
  • 前往 Manage SQL Schemas 页面。
  • 编辑模式。
  • 使用 Atlas CLI 编辑模式
  • 语法
  • 参数
  • 输出
  • 举例
  • 设置模式示例
  • 删除模式示例

要编辑现有模式,请执行以下步骤。

1
  1. 如果尚未显示,请从导航栏的 Organizations 菜单中选择包含所需项目的组织。

  2. 如果尚未显示该页,请从导航栏的 Projects(项目)菜单中选择所需的项目。

  3. 如果 Clusters(数据库部署)页面尚未出现,请单击侧边栏中的 Database(数据库)。

2

从 Data Federation 菜单中,单击Manage SQL Schemas

3
  1. 在模式旁边,单击

  2. 编辑 JSON。

  3. 单击 Save(连接)。

sqlSetSchema命令设置或删除集合或视图的模式。该命令使用提供的模式创建关系模式。该命令不会根据集合中的数据验证命令提供的模式。

范围
类型
说明
必要性
<collection-name>
字符串
要为其设置模式的集合的名称。您必须提供集合名称或视图名称。
可选的
<view-name>
字符串
要为其设置模式的视图的名称。您必须提供视图名称或集合名称。
可选的
schema
文档
模式的格式版本以及以下任一项:
  • 集合或视图的 JSON schema

  • 空文档,用于删除集合或视图的模式。

注意

您可以在items字段中提供单个文档或文档数组。 检索模式时, items会显示您用于设置模式的表单。

必需

如果成功,该命令将返回以下输出。

{ "ok" : 1 }

您可以通过运行sqlGetSchema命令来验证该命令是否成功。响应中的metadata.description字段包含以下值:

"set using sqlSetSchema"

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

以下示例使用sqlSetSchema命令设置和删除上述collection的模式。

以下sqlSetSchema命令为egDatacollection设置模式。

db.runCommand({
sqlSetSchema : "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" ],
"properties" : {
"b" : {
"bsonType" : [ "object" ],
"properties" : {
"c" : {
"bsonType" : [ "object" ],
"properties" : {
"d" : {
"bsonType" : [ "array" ],
"items" : {
"bsonType" : [ "string" ]
}
}
}
}
}
}
}
}
}
}
}
})

上一个命令会返回以下输出。

{ "ok" : 1 }

以下sqlSetSchema命令删除egDatacollection的模式。

db.runCommand({
sqlSetSchema: "egData",
schema: {}
})

上一个命令会返回以下输出。

{ "ok" : 1 }
← 查看模式