编辑模式
在此页面上
使用 Atlas 用户界面编辑模式
要编辑现有模式,请执行以下步骤。
使用 Atlas CLI 编辑模式
sqlSetSchema
命令设置或删除集合或视图的模式。该命令使用提供的模式创建关系模式。该命令不会根据集合中的数据验证命令提供的模式。
语法
参数
范围 | 类型 | 说明 | 必要性 |
---|---|---|---|
<collection-name> | 字符串 | 要为其设置模式的集合的名称。您必须提供集合名称或视图名称。 | 可选的 |
<view-name> | 字符串 | 要为其设置模式的视图的名称。您必须提供视图名称或集合名称。 | 可选的 |
schema | 文档 |
注意您可以在 | 必需 |
输出
如果成功,该命令将返回以下输出。
{ "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
命令为egData
collection设置模式。
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
命令删除egData
collection的模式。
db.runCommand({ sqlSetSchema: "egData", schema: {} })
上一个命令会返回以下输出。
{ "ok" : 1 }