Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Studio 3T Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

Schema Validator

Posted on: 23/11/2018 (last updated: 31/08/2021) by Kathryn Vargas
tl;dr

Use Schema Validator to apply JSON Schema, the recommended way of performing MongoDB schema validation to a collection. Get started today.

Add MongoDB schema validation

We’ll be applying schema to the Customers collection, which you can download here.

  1. Right-click on your target connection (e.g. Customers)  in the Connection Tree.
  2. Choose Add/Edit Validator. This will open the Edit Validator window.
Right-click on a collection and choose Add/Edit Validator

Paste JSON Schema validation rules

In the Edit Validator window, type in or paste your validation rules using JSON Schema.

Apply JSON schema validation rules to your collection

Example of JSON Schema

Below is the JSON Schema we applied to the Customers collection:

{ 
    "bsonType" : "object", 
    "required" : [
        "title", 
        "first", 
        "last", 
        "email", 
        "dob", 
        "address.street.name", 
        "address.street.suffix", 
        "address.street.number", 
        "address.city", 
        "address.state", 
        "address.zip-code", 
        "user_name", 
        "package", 
        "prio_support", 
        "registered_on", 
        "transactions", 
        "pet", 
        "number_pets", 
        "Studio_3T_edition", 
    ], 
    "properties" : {
        "title" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "first" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "last" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "email" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "dob" : {
            "bsonType" : "date", 
            "description" : "must be a date and is required"
        }, 
        "address.street.name" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "address.street.number" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "address.street.suffix" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "address.city" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "address.state" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "address.zip_code" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "user_name" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "package" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "prio_support" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "registered_on" : {
            "bsonType" : "date", 
            "description" : "must be a date and is required"
        }, 
        "transactions" : {
            "bsonType" : "int32", 
            "description" : "must be an integer and is required"
        }, 
        "pet" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "number_pets" : {
            "bsonType" : "int32", 
            "description" : "must be an integer and is required"
        }, 
        "Studio_3T_edition" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }
    }
}

Set validation levels

Set the validation to off, strict, or moderate directly in Studio 3T

You can choose how MongoDB applies your validation rules:

  • off – MongoDB disables validation completely
  • strict (default setting) – MongoDB applies validation rules to all inserts and updates
  • moderate – MongoDB applies validation rules to inserts and to updates to existing documents that already fulfill the validation criteria

Set validation action

Set the validation action to error or warn

MongoDB can handle documents that don’t meet the validation rules in two ways:

  • error (default setting) – Rejects inserts or updates that do not meet validation rules
  • warn – Allows inserts and updates while logging violations

The error messages don’t tell you what failed, even though MongoDB records the validation violations.

JSON schema validation error message

For further debugging, Studio 3T‘s Schema Explorer feature and other third-party JSON schema validation tools can come in handy, which we explain in detail in this tutorial.

Validate and save validation rules

Validate JSON before saving your MongoDB schema

Click on Validate to make sure the JSON is correct, then Save.

JSON Schema validation tutorial

Want to dive deeper into this topic? Read our tutorial, How to Create and Validate JSON Schema in MongoDB Collections, to learn more about how you can use SQL, Schema Explorer, and JSON Schema validation tools to help create your MongoDB schema.


How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Kathryn Vargas

When she's not writing about working with MongoDB, Kathryn spends her free time exploring Berlin's food scene, playing the drums, learning languages (current mission: German), and hiking.

Article navigation

Related articles

  • How do I clean up a dataset and schema with Studio 3T? – #Studio3T_AMA
  • More Dataset And Schema Clean Ups With Studio 3T – #Studio3T_AMA
  • How do I analyze the schema of some of my documents. #Studio3T_AMA

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2023 3T Software Labs Ltd. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum

We value your privacy

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data.

By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
Accept all
Decline all
Manage cookies
✕

Privacy Preference Center

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes ("analytics and marketing") and to provide you with additional functions (“functional”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in our Privacy Policy.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Google Analytics

Google Ads

Bing Ads

Facebook

LinkedIn

Quora

Hotjar

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

HubSpot

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected