0% found this document useful (0 votes)
42 views31 pages

02 - Datamodelling With MongoDB - Schuelerversion

The document discusses data modeling concepts for document-based databases like MongoDB. It covers topics such as describing entities and relationships, validating data, logical concepts like collections and documents, and basic modeling paradigms such as embracing duplication, avoiding joins, and working backwards from use cases to data models. Guidelines are provided for deciding between embedding data versus referencing in other collections. Sample modeling exercises are also included for a class timetable and teacher schema.

Uploaded by

Maxi Busch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views31 pages

02 - Datamodelling With MongoDB - Schuelerversion

The document discusses data modeling concepts for document-based databases like MongoDB. It covers topics such as describing entities and relationships, validating data, logical concepts like collections and documents, and basic modeling paradigms such as embracing duplication, avoiding joins, and working backwards from use cases to data models. Guidelines are provided for deciding between embedding data versus referencing in other collections. Sample modeling exercises are also included for a class timetable and teacher schema.

Uploaded by

Maxi Busch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Data Modelling with

MongoDB
5th grade / V. Jahrgang

Quellen: linkedin
Aspects of Data Modelling
• Describing Entities
• Describing Relationsships
• Validatation of Data
• „Translation“ of these ideas into a logical concept to fullfill these
aspects (f.ex. Relational datamodel)

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 2


noSQL-View on Data Modelling
• Describing Entities by describing objects and defining their properties
and the methods to change them
• Describing Relationsships
• Validata Data
• „Translation“ of these ideas into a logical concept to fullfill these
aspects , f.ex
• Json or xml
• Graphs and distances

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 3


Logical concepts of Document based
Databases
• Collections
• Documents
• Subdocuments/embedded documents
• Arrays
• Document centric view = document is the single source of truth
• No/little normalization
• Joins are rare
• Depth is okay

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 4


Basic Paradigmas of modelling Document
based Databases
• No/little normalization
• Duplication is acceptable „embrace duplication“
• Joins/References are rare
• Depth is okay (=Arrays, embedded objects and combinations of both)
• Reading operations are „cheap“ (fast), updating operations and joins are
expensive (slow)
• Difference between static and dynamic properties is important
• While modelling think in big scale
• Joins are bottlenecks but better than frequent update processes on large scale
• Work backwards = from use case to data model
• Are there relationships (1:n, m:n)
Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 5
Basic Paradigmas of modelling Document
based Databases
• No/little normalization
• Duplication is acceptable „embrace duplication“
• Joins are rare
• Depth is okay (=Arrays, embedded objects and combinations of both)
• Duplication is okay (f.ex. To make queries faster)
• Reading operations are „cheap“ (fast), updating operations and joins are
expensive (slow)
• Difference between static and dynamic properties is important
• While modelling think in big scale
• Joins are bottlenecks but better than frequent update processes on large scale
• Work backwards = from use case to data model
• „One query for one page“-objective (or at least very little queries)
Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 6
Reasons for external References/FK
• Multiple documents reference the same object
• Object has some volatility → updating would be necassary for all
these references → would be very expensive
• Embedded (reduced) information should be accesible for deeper look
on data (drill-in)

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 7


Solutions for 1:n-Relations
• Embedding in an array:
• Could become giant (16MB-limit)
• Extract subdocument to proper collection and reference it bei
$lookup
• At bigger scale very „expensive“
• Summary fields to prefetch a certain subset and reference for details

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 8


Handeling versions
• In case of changing requirements changes are much easier than on
relational DB/DDL-structure.
• Recomandations:
• Add version of „schema“ to handle different fields on the backend
• use $jsonschema to define requirements that are absolutely necassary

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 9


Enforcing datamodel by using $jsonschema
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
title: "Student Object Validation",
required: [ "address", "major", "name", "year" ],
properties: {
name: {
bsonType: "string",
description: "'name' must be a string and is required"
},…} )#

https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/#std-label-schema-validation-json
Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 10
$jsonschema enforcing certain values
db.createCollection("shipping", {
validator: {
$jsonSchema: {
bsonType: "object",
title: "Shipping Country Validation",
properties: {
country: {
enum: [ "France", "United Kingdom", "United States" ],
description: "Must be either France, United Kingdom, or
United States" } } } }} )

https://www.mongodb.com/docs/manual/core/schema-validation/specify-json-schema/specify-allowed-field-values/#std-label-schema-allowed-field-values

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 11


Take Care!
• Document based databases are not slimmer (many redundancies)
• For fundamental noSQL-benefits (speed, cost, scalability) they need
as much or even more pre-definitions and modelling
• Prepare and model your most important „views“ beforehand
• Plan scalibility by modelling logical and therefore partitionable units (classes)
• Know the focus of your application (write-load, read-load)

NoSQL is not a „quick“ substitue for relational


databases!!

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 12


Guidelines embedding versus referencing
• Many reading processes
• Reading processes are frequently
in the combination of super- and
sub-document
• Sub-documents have „natural“
limit („one-to-few“-relations)
• Many static data sets/entities
EMBEDDING

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 13


Guidelines embedding versus referencing
• Many writing processes →
• No practical limit to document
size (f.ex. unlimmited
commentary possibilites on
posting) →

REFERENCING

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 14


Use Cases

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 15


Use Case „E-Commerce Website“
• Use Case Wish List
• Use Case Shopping Card
• Use Case Order History

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 16


Assignment1: Evaluation of Use Cases
1) Define the objects involved in this use case with their attributes
• Class definition (OOP) OR
• Highlight and summarize them on print out
2) Which of the data do you consider to be
• Static
• Dynamic

Means of modelling: object definition (1), arrays, embedded documents

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 17


A1 Sample:
Timetable per class
1. Class: perspective of view →
json-file „class“
2. Headmaster: (very likely)
static | single value →
Attribute of class
3. Week: static | array of values
→ embedding into class
4. Sessions per hour: (most
times) static | limited amount
of informational unit „lesson“
with attributes name, teacher,
room, further specifications
→ embedded object per day
14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 18
A1 Sample:
Timetable sample json
1. File per class
2. Embedded teacher (object)
3. Many lessons per teacher → sessions could
be embedded into teacher
4. Sessions are repeated per week, so weekday
could be embedded into sessions

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 19


Assignment2: Defining Schema
1) Define a propriate json-Schema for your use-case
2) Implement the schema on your mongoDB
3) Model a class covering the needs of your e-Formular on your
MongoProject (Java, C#, …)
4) Test your schema by inserting valid and invalid test data

5) Bonus: develop a secondary version of your schema and define a


possible handling of it on a query example.

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 20


A2 Sample:
$jsonschema
{ %%,
"$schema": "http://json-schema.org/draft-04/schema#", "Lehrer": { "type": "array",
"type": "array", "items": [ {
"items": [{ "type": "object",
"type": "object", "properties": {
"properties": { "Id": { "type": "string" },
"Id": { "type": "string" }, "Zuname": { "type": "string"},
"Kv": { "type": "object", "Vorname": { "type": "string" },
"properties": { "Gegenstaende": { "type": "array",
"Id": { "type": "string}, "items": [ { "type": "object",
"Zuname": { "type": "string" }, "properties": {"Id": {
"Vorname": { "type": "string“ } }, "type": "string" }, "Name": {
"required": [ "Id", "Zuname", "Vorname" ] "type": "string"
%% }, "Zeiten": { "type": "array",
"items": [ { "type": "object", "properties": {

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 21


Assignment3: Introducing Referencing
1) Identify drivers for referencing
1) (potentially) dynamic datasets
2) write-heavy processes
3) Unlimited arrays

2) Alter the model to (partially) referenced structures

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 22


E-Formular: Eignungstest

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 23


E-Formular: Abschlussprüfungen

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 24


E-Formular: Schulbesuchsbestätigungen

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 25


E-Formular: Wahlpflichtfächer

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 26


E-Formular: Religionsanmeldung

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 27


E-Formular: Zeugnis/Aufstieg

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 28


E-Formular: Zeugnis/Nachprüfung

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 29


E-Formular: Sommerschule

Status : 14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 30


Sources
• John Cokos:
https://www.linkedin.com/learning/data-
modeling-in-mongodb/introduction-to-
data-
modeling?autoplay=true&u=37766780
• E-Formular: e-formular.spengergasse.at
• MongoDB-Doku:
https://www.mongodb.com
• https://learn.microsoft.com/en-
us/azure/cosmos-db/nosql/modeling-
data

"Dieses Foto" von Unbekannter Autor ist lizenziert gemäß CC BY-SA

14.12.2022 DI(FH) Mag. Johanna Maria NIKLAS 31

You might also like