Agenda• Part 1 (20-25 mins)
• Introduction• MongoDB data modeling overview• Types of data models• What is M2?• When to use M2?• M2 technology
• Part 2 (20-25 mins)• M2 demonstration
• Show website• Public models• Explore• Documentation• Registration
• Show some public models• Product (embedded)• Referenced One-to_many V2 (referenced)• Atomic Operation (hybrid)
• Show M2 features• Create one-to-few, one-to-many, and one-to-squillion models (based on patterns from William Zola’s MongoDB blog post)
• Q & A
Part 1
Introduction• Flexible schema
• no need to determine and declare structural data elements before inserting data
• Document-based data model• Collections, documents, fields, references
• Document structure is not enforced what enables mapping of documents to any object (entity)
• While it is not required, it is recommended to have uniform document structure across single collections.
Introduction (cont.)
• Atomicity• There is no concept of transaction in MongoDB• All operations that create or change data (e.g., write, update,
delete) are atomic at the document level only
• MongoDB does not support joins.
• MongoDB is designed to take care of many performance aspects by itself but it leaves to designers and developers to figure out the best possible data model for the specific application patterns.
Introduction (cont.)• It may come as a surprise to some people, but as Asya
Kamsky wrote in her 10 Things You Should Know About Running MongoDB at Scale article, on the High Scalability portal, the biggest impact to performance is “how well the schema design fits with the application needs“. Asya came to this conclusion after having seen hundreds of production deployments. This is a fairly firm statement that proves the importance of data modeling for MongoDB-based systems.
Data Modeling Overview• MongoDB data models created in M2 includes
• Collections (analogous to JSON objects, RDBMS tables)• Documents (analogous to RDBMS table rows)• Fields (analogous to RDBMS table columns)• References (analogous to RDBMS foreign keys)
Model
Collection
Document
FieldField
Reference
Data Modeling Overview (cont.)
_idparentId
ManualReference
Database A
_idparentId
Database A Database B
DBRef
Key Considerations with Data Modeling
• Read and write operations
• Document growth
• Automicity
• Sharding
Types of Data Models• There are several types of MongoDB data models you can create:
• Embedding Model• Referencing Model• Hybrid Model that combines embedding and referencing models.
• Embedding model enables de-normalization of data, which means that two or more related pieces of data will be stored in a single document. Frequently it is a choice for “contains” and “one-to-many” relationships between entities (documents).• Related data can be fetched with a single query• Duplication of data; multi-document changes• It is a better choice for
• atomicity• smaller documents;• data does not change or does not grow much;• data that you usually fetch together;• fast reads
Types of Data Models (cont.)• Referencing model enables normalization of data by storing references
between documents to indicate a relationship between the data stored in each document. • No duplication of data• Only one document change is required to change data• No joins. Retrieving data from multiple documents requires multiple queries to be done
by an application.• It is a better choice for
• when embedding would result in extensive data duplication (but would not provide a significant read performance advantage) and/or data fragmentation when embedded documents grow;
• to present more complex “many-to-many” relationships;• to model large hierarchical data sets.• fast writes
• Hybrid model is a combination of embedding and referencing model. It is usually used when neither embedding or referencing model is the best choice but their combination makes the most balanced model.
Data Modeling Overview (cont.)
Collection C1
Doc D2
Doc D1
Collection C1 Collection C2
Doc D1 Doc D2
Embedding Model
Referencing Model
What is M2?• M2 is a cloud service for the creation and management of
MongoDB data models• M2 features include:
• Management of models and their elements (collections, documents, fields and references)
• Copying and versioning of models, collections and documents via related Copy utilities
• Multiple document formats for a collection• Full models' view in a JSON-like format (M2 notation)• Export/Import Models • Public models sharing • Models documentation repository • Messaging between M2 users
When to use M2?• Analysis and design are important steps in an overall
engineering process of system architecture, design, development and implementation/deployment. M2 is used for a data model (schema) design.
• Up-to-date data model management with an on-line (cloud) service capability.
• Educative purposes. Public data model samples based on various design patterns and best practices.
When to use M2? (cont.)• Models can be documented via documentation stored and
managed in M2 documentation repository or referenced via external links.
• Free service. More plans will be available in near future.
• Code generation (future releases)• Standardizing (based on M2 data models) and automating the writing of
data access code (from M2 data models) can save significant developer time and make the resulting code better defined and easier to maintain and to interface with.
• Model visualisation (future releases)
M2 Technology• MongoDB• Java, Java Servlets, JSP, jQuery• Tomcat• Red Hat Enterprise Linux
Part 2(M2 Demonstration)
How to Access M2?• http://www.daprota.com• https://m2.daprota.com
One-to-Few
> db.person.findOne(){ _id: ObjectId(‘ABC’), name: ‘John Smith', sin: '123-456-7890', addresses : [ { street: '123 Sesame St', city: ‘Toronto', cc: ‘CA' }, { street: '123 Avenue Q', city: 'New York', cc: 'US' } ]}
One-to-Many> db.parts.findOne(){ _id : ObjectId('AAA'), partno : '123-aff-456', name : '#4 grommet', qty: 94, price: 3.99}
> db.products.findOne(){ _id : ObjectId(‘BBB’), name : 'left-handed smoke shifter', manufacturer : 'Acme Corp', catalog_number: 1234, parts : [ // array of references to Part documents ObjectId('AAA'), // reference to the #4 grommet above ObjectId('F17'), // reference to a different Part ObjectId('D2A'), // etc ]
One-to-Squillions> db.hosts.findOne(){ _id : ObjectId('AAAB'), name : 'goofy.example.com', ipaddr : '127.66.66.66'}
>db.logmsg.findOne(){ time : ISODate("2014-03-28T09:42:41.382Z"), message : 'cpu is on fire!', host: ObjectId('AAAB') // Reference to the Host document}
Documentation and Books• M2 Documentation
http://www.daprota.com/doc/m2/m2-documentation.html
• Data Models (MongoDB Documentation) http://docs.mongodb.org/manual/data-modeling/
• MongoDB: The Definitive Guide by Kristina Chodorow (2013)
• MongoDB Applied Design Patterns by Rick Copeland (2013)
• MongoDB in Action by Kyle Banker (2011)
Q&A
Thank you!Goran [email protected]://www.daprota.comhttps://m2.daprota.com@DaprotaM2http://goranzugic.wordpress.com/http://www.goranzugic.com/