SlideShare a Scribd company logo
#MDBlocal
Just in Time Validation with
JSON Schema
#MDBlocal
Shannon Bradshaw
VP of Developer Education,
Snowboarding Enthusiast, Father
MongoDB
@shannonbradshaw
#MDBlocal
$jsonSchema
#MDBlocal
Flexible
Schema
Rigid
Schema
Your App
#MDBlocal
Flexible
Schema
Rigid
Schema
Your App
risk
success!
regulation
policy
politics
#MDBlocal
Flexible
Schema
Rigid
Schema
Your App
risk
success!
regulation
policy
politics
Production
Concerns
#MDBlocal
#MDBlocal
{
_id: 34556,
item: "Flannel T-shirt",
price: NumberDecimal("27.99"),
quantity: NumberInt(1)
color: "green"
}
Simple Schema to Start
#MDBlocal
Document Validation (v3.2)
Use query filters to define schema constraints. E.g.,
db.createCollection("orders", {
validator: {
item: {$type: "string"},
price: {$type: "decimal"},
quantity: {$type: "int"},
color: {$in: ["red", "green", "blue"]}
}
});
(ex1 - ex6)
#MDBlocal
JSON Schema (v3.6)
● JSON Schema Draft 04
● Is becoming an industry standard
● Widely used; lots of community support
(ex5)
#MDBlocal
Flexible
Schema
Rigid
Schema
Our App
#MDBlocal
(ex6 - ex8b)
Let's make our validator a little
more strict
#MDBlocal
Good for Starter Examples
{
_id: 34556,
item: "Flannel T-shirt",
price: NumberDecimal("27.99"),
quantity: NumberLong(1),
color: "green"
}
{
_id: 34556,
total: NumberDecimal("141.00"),
VAT: NumberDecimal("0.20"),
totalWithVAT: NumberDecimal("169.20"),
lineitems: [
{ sku: "MDBTS001",
name: "Flannel T-shirt",
quantity: 10,
unit_price: NumberDecimal("9.00") },
{ sku: "MDBTS002",
quantity: 5,
unit_price: NumberDecimal("10.00")}]
}
More Realistic
#MDBlocal
Good for Starter Examples
{
_id: 34556,
item: "Flannel T-shirt",
price: NumberDecimal("27.99"),
quantity: NumberLong(1),
color: "green"
}
{
_id: 34556,
total: NumberDecimal("141.00"),
VAT: NumberDecimal("0.20"),
totalWithVAT: NumberDecimal("169.20"),
lineitems: [
{ sku: "MDBTS001",
name: "Flannel T-shirt",
quantity: 10,
unit_price: NumberDecimal("9.00") },
{ sku: "MDBTS002",
quantity: 5,
unit_price: NumberDecimal("10.00")}]
}
More Realistic
#MDBlocal
Our New Orders Schema
● Orders must contain a lineitems array of 1 to 10 documents
● Documents embedded in lineitems have fields for:
○ sku (required)
○ name (optional)
○ unit_price (required)
○ quantity (required)
● Value of quantity must be an integer
● (Must also contain fields: total, VAT, totalWithVAT, but we'll get to that.)
#MDBlocal
Let's validate the line item documents
(embedded in an array)
(ex9 - 10b)
#MDBlocal
Now Let's Deal with Totals and VAT
The most recent order we looked at actually contains a few errors:
● The totalWithVAT value should be 141 * 1.20 = 169.2
● The total value should be the sum of each line item sub-total
● I.e., (10 * 9) + (10 * 5) = 140
Can we enforce that totalWithVAT and total are correct using validation?
#MDBlocal
Yes we can!
● MongoDB 3.6 -- more expressive query syntax
● Can express complex business rules in validations
● E.g, compute and compare multiple values at runtime
#MDBlocal
$expr
Check that totalWithVAT == total * (1+VAT):
$expr: {
$eq: [
"$totalWithVAT",
{$multiply: [
"$total",
{$sum: [1, "$VAT"]}
]}
]
}
Let's add this as a validation rule alongside our JSON Schema validation ()
#MDBlocal
Now let's make sure the total is the
sum of the line item subtotals
#MDBlocal
Ensure total equals sum of line items
$expr: {
$eq: [
"$total",
{$sum: {
$map: {
"input": "$lineitems",
"as": "item",
"in": {
"$multiply": [
"$$item.quantity",
"$$item.unit_price"
] } } }} ] }
#MDBlocal
All together now
(ex11 - 14c)
#MDBlocal
Flexible
Schema
Rigid
Schema
Our App
#MDBlocal
Flexible
Schema
Rigid
Schema
risk
success!
regulation
policy
politics
Our App
#MDBlocal
Flexible
Schema
Rigid
Schema
risk
success!
regulation
policy
politics
Our App
Production
Concerns
#MDBlocal
Special thanks to Raphael Londner
#MDBlocal
Learn More
● What's New Guide
● MongoDB Documentation
○ Schema Validation
○ Expressive Query Syntax
● Free MongoDB 3.6 Online Course from MongoDB University

More Related Content

PPTX
"Powerful Analysis with the Aggregation Pipeline (Tutorial)"
PPTX
ETL for Pros: Getting Data Into MongoDB
PDF
Doing More with MongoDB Aggregation
PDF
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
PDF
MongoDB .local Chicago 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
PDF
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
PPTX
Aggregation Framework
PDF
MongoDB World 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pipeline Em...
"Powerful Analysis with the Aggregation Pipeline (Tutorial)"
ETL for Pros: Getting Data Into MongoDB
Doing More with MongoDB Aggregation
MongoDB .local Toronto 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local Chicago 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pi...
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
Aggregation Framework
MongoDB World 2019: Aggregation Pipeline Power++: How MongoDB 4.2 Pipeline Em...

What's hot (20)

PDF
MongoDB Aggregation Framework
PDF
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
PPTX
Aggregation in MongoDB
PDF
Embedding a language into string interpolator
PDF
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
PPTX
The Aggregation Framework
PPTX
The Aggregation Framework
KEY
MongoDB Aggregation Framework
PPTX
Webinar: Exploring the Aggregation Framework
PDF
Aggregation Framework MongoDB Days Munich
PDF
MongoDB .local Bengaluru 2019: Aggregation Pipeline Power++: How MongoDB 4.2 ...
PPTX
Agg framework selectgroup feb2015 v2
PPTX
Powerful Analysis with the Aggregation Pipeline
PPTX
MongoDB World 2016 : Advanced Aggregation
ODP
Aggregation Framework in MongoDB Overview Part-1
PPTX
MongoDB Aggregation
PDF
MongoDB Europe 2016 - Debugging MongoDB Performance
PDF
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
PPTX
[MongoDB.local Bengaluru 2018] Tutorial: Pipeline Power - Doing More with Mon...
PDF
MongoDB Europe 2016 - Graph Operations with MongoDB
MongoDB Aggregation Framework
MongoDB Europe 2016 - Advanced MongoDB Aggregation Pipelines
Aggregation in MongoDB
Embedding a language into string interpolator
MongoDB Europe 2016 - ETL for Pros – Getting Data Into MongoDB The Right Way
The Aggregation Framework
The Aggregation Framework
MongoDB Aggregation Framework
Webinar: Exploring the Aggregation Framework
Aggregation Framework MongoDB Days Munich
MongoDB .local Bengaluru 2019: Aggregation Pipeline Power++: How MongoDB 4.2 ...
Agg framework selectgroup feb2015 v2
Powerful Analysis with the Aggregation Pipeline
MongoDB World 2016 : Advanced Aggregation
Aggregation Framework in MongoDB Overview Part-1
MongoDB Aggregation
MongoDB Europe 2016 - Debugging MongoDB Performance
MongoDB .local Paris 2020: La puissance du Pipeline d'Agrégation de MongoDB
[MongoDB.local Bengaluru 2018] Tutorial: Pipeline Power - Doing More with Mon...
MongoDB Europe 2016 - Graph Operations with MongoDB
Ad

Similar to Data Governance with JSON Schema (20)

PPTX
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
PDF
MongoDB World 2019: Just-in-time Validation with JSON Schema
PPTX
How to leverage what's new in MongoDB 3.6
PDF
Agile Database Development with JSON
PPTX
SH 1 - SES 2 part 2 - Tel Aviv MDBlocal - Eliot Keynote.pptx
PPTX
SH 1 - SES 2 part 2 - Tel Aviv MDBlocal - Eliot Keynote.pptx
PPTX
Keynote - Speaker: Grigori Melnik
PDF
MongoDB .local Paris 2020: Tout savoir sur le moteur de recherche Full Text S...
PPTX
Data Analytics with MongoDB - Jane Fine
PDF
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
PPTX
MongoDB World 2018: Keynote
PPTX
How to Leverage APIs for SEO #TTTLive2019
PPTX
[MongoDB.local Bengaluru 2018] Keynote
PPTX
Query for json databases
PDF
MongoDB Meetup
PDF
MongoDB .local Munich 2019: Still Haven't Found What You Are Looking For? Use...
PPTX
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
PDF
MongoDB.local Paris Keynote
PDF
MongoDB .local Chicago 2019: Still Haven't Found What You Are Looking For? Us...
PDF
Advanced Analytics & Statistics with MongoDB
[MongoDB.local Bengaluru 2018] Just in Time Validation with JSON Schema
MongoDB World 2019: Just-in-time Validation with JSON Schema
How to leverage what's new in MongoDB 3.6
Agile Database Development with JSON
SH 1 - SES 2 part 2 - Tel Aviv MDBlocal - Eliot Keynote.pptx
SH 1 - SES 2 part 2 - Tel Aviv MDBlocal - Eliot Keynote.pptx
Keynote - Speaker: Grigori Melnik
MongoDB .local Paris 2020: Tout savoir sur le moteur de recherche Full Text S...
Data Analytics with MongoDB - Jane Fine
MongoDB.local DC 2018: Tutorial - Data Analytics with MongoDB
MongoDB World 2018: Keynote
How to Leverage APIs for SEO #TTTLive2019
[MongoDB.local Bengaluru 2018] Keynote
Query for json databases
MongoDB Meetup
MongoDB .local Munich 2019: Still Haven't Found What You Are Looking For? Use...
Back to Basics Webinar 4: Advanced Indexing, Text and Geospatial Indexes
MongoDB.local Paris Keynote
MongoDB .local Chicago 2019: Still Haven't Found What You Are Looking For? Us...
Advanced Analytics & Statistics with MongoDB
Ad

More from MongoDB (20)

PDF
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
PDF
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
PDF
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
PDF
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
PDF
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
PDF
MongoDB SoCal 2020: MongoDB Atlas Jump Start
PDF
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
PDF
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
PDF
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
PDF
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
PDF
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
PDF
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
PDF
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
PDF
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
PDF
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
PDF
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
PDF
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
PDF
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
KodekX | Application Modernization Development
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The AUB Centre for AI in Media Proposal.docx
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Review of recent advances in non-invasive hemoglobin estimation
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
KodekX | Application Modernization Development
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
Diabetes mellitus diagnosis method based random forest with bat algorithm
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Data Governance with JSON Schema