The schema
The use of JSON Schema is a feature of the paid version of Mongo Management Studio.
A highlight of Mongo Management Studio is the optional JSON schema validation. This is based on the module lx-valid and allows the validation of documents based on data types and formats. Because MongoDB stores the data without a schema, you can save what you want in a document in each field. This is critical especially in production use databases. The activated JSON schema validation can for example prevent a MongoDB ObjectId beeing accidentally saved as a string or an object as a number. The validation is available when creating and editing documents.
The validation using the schema is optional and disabled in default settings.
If you load a collection, a schema based on the documents obtained from the query is automatically generated, except there is already a stored schema, then this is loaded. In the "JSON schema" tab you can view and edit the schema. If you have changed the schema, you can save it by clicking on the "Save schema" button. Click the "Generate new schema" button to re-create the schema based on all documents in the collection.
Another aspect is the syntax of the schema. It is designed so that the user is able to copy the schema from the MongoDB Management Studio to own applications using copy/paste and work with it. For example the module lx-mongodb uses the JSON schema to store data in MongoDB. The user is able to edit the schema using test data before using it in applications with database operations in production and gain security of data types with it.
The schema for a collection is stored within Mongo Management Studio and not in the database. It is only used within the application and does not affect the use of databases outside this application.
Structure of the schema in the example of the zipcode-collection
If you went through the Getting started tutorial, you have a database demo including the zipcode collection. When opening the collection and editing a document, you can see the schema by clicking the "JSON Schema" button.
Type
Type determines the data type of a field. The following data types are supported:
string
array
object
number
integer
float
boolean
null
date
regexp
any
If you try to fill the "city" field with a value of 23, you receive an error message. The "city" field is of type "string" and "23" is not a string.
Fields can have multiple data types within a collection. For example, if in a document the "pop: null" field is saved without validation, the schema changes on a new find-query as follows:
The field „pop“ can be of type Null or Integer now.
Format
Some data types have special formatting in the schema. These types include the MongoDB ObjectId or date values. Because Mongo Management Studio transmits the data in JSON, such data types are always converted to a string. Thus, to still store the correct data types in the mongo database, the "string" type with an additionally specified format is used here. Find all supported formats here https://github.com/litixsoft/lx-valid#validation-formats.
// MongoDB ObjectId
_id: {
type: 'string',
format: 'mongo-id'
}
// Datum
start: {
type: 'string',
format: 'date-time'
}
Required
Is “required” set to true, the validation of the document can only be successful when the corresponding field exists. Is the field missing, an error is issued.
In our example, the "city" field with the "required" option is provided. If you try to save the document without a "city"-field, you get an error message. The document was not saved because the "city"-field is empty.