Edit Schema
On this page
Edit Schema Using the Atlas UI
To edit an existing schema, use the following steps.
In Atlas, go to your federated database instance for your project.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Data Federation under the Services heading.
The Data Federation page displays.
Edit Schema Using mongosh
The sqlSetSchema
command sets or deletes the schema for a collection
or view. The command uses the provided schema to create the relational
schema. The command doesn't validate the schema provided with the command
against the data in the collection.
Syntax
db.getSiblingDB("<dbName>").runCommand({ sqlSetSchema: "<collection-name>|<view-name>", schema: { "version": 1, "jsonSchema": <jsonSchema> } })
db.getSiblingDB("<dbName>").runCommand({ sqlSetSchema: "<collection-name>|<view-name>", schema: {} })
Parameters
Parameter | Type | Description | Necessity |
---|---|---|---|
| string | Name of the collection for which to set the schema. You must provide either a collection name or a view name. | Conditional |
| string | Name of the view for which to set the schema. You must provide either a view name or a collection name. | Conditional |
| document |
You can provide a single document or an array of documents
in the | Required |
Output
The command returns the following output if it succeeds.
{ "ok" : 1 }
You can verify that the command succeeded by running the
sqlGetSchema
command. The
metadata.description
field in the response contains the following value:
"set using sqlSetSchema"
Examples
Consider a collection named egData
in a database named
sampleDB
with the following documents:
{"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}
The examples below use the sqlSetSchema
command to set
and remove schema for the above collection.
Set Schema Example
The following sqlSetSchema
command sets the schema for
the egData
collection.
db.getSiblingDB("sampleDB").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" ] } } } } } } } } } } } })
The previous command returns the following output.
{ "ok" : 1 }
Delete Schema Example
The following sqlSetSchema
command removes the schema
for the egData
collection.
db.getSiblingDB("sampleDB").runCommand({ sqlSetSchema: "egData", schema: {} })
The previous command returns the following output.
{ "ok" : 1 }