Download - Application Design per MongoDB
![Page 1: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/1.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Application Design
FOR MongoDB
Alessandro [email protected]
http://it.linkedin.com/in/alessandropalumbo/http://www.byte-code.com
Saturday, November 30,
![Page 2: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/2.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB
Saturday, November 30,
![Page 3: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/3.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB
NoSql
Saturday, November 30,
![Page 4: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/4.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB
NoSql
OPEN-source
Saturday, November 30,
![Page 5: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/5.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB
NoSql
OPEN-source
Document-OrientedJSON-style documents
Saturday, November 30,
![Page 6: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/6.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB
from humongous “huge; enormous”
NoSql
OPEN-source
Document-OrientedJSON-style documents
Saturday, November 30,
![Page 7: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/7.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
JSON-style documents
{ "_id" : "6c85fa4c-fa64-44e2-89c9-e5eb7f306ed7", "code" : "CRS0001", "name" : "Test", "description" : "Test description", "active" : true, "scheduledDate" : { "from" : ISODate("2013-09-12T00:00:00.000Z"), "to" : ISODate("2013-10-31T00:00:00.000Z") }, "version" : NumberLong(1)}
Saturday, November 30,
![Page 8: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/8.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
Saturday, November 30,
![Page 9: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/9.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
Saturday, November 30,
![Page 10: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/10.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
NO FULL transactions
Saturday, November 30,
![Page 11: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/11.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
NO FULL transactions
no SCHEMA
Saturday, November 30,
![Page 12: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/12.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
NO FULL transactions
no SCHEMA
WE CAN EMBED
Saturday, November 30,
![Page 13: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/13.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
NO FULL transactions
no SCHEMA
WE CAN EMBED
DOCUMENT LEVELTRANSACTIONS
Saturday, November 30,
![Page 14: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/14.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
don’t be relationaL
no joins
NO FULL transactions
no SCHEMA
WE CAN EMBED
IS IT REALLY AN ISSUE?
DOCUMENT LEVELTRANSACTIONS
Saturday, November 30,
![Page 15: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/15.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Saturday, November 30,
![Page 16: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/16.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
friendly fire(aka RTFM)
Write
Concern
READ
PREFERENCE
ATOMIC
DOCUMENT
OPERATIONS
AVOID
NATURAL
KEYS ASIDENTIFIERS
Saturday, November 30,
![Page 17: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/17.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DESIGN
DESIGN
FOR
QUERYEMBEDDED
DATA
vsReferences
DBREFS
VS
MANUALREFERENCE
DYNAMIC
SCHEMA
VSstatic
languages
PURE DRIVER
VS
MAPPINGFRAMEWORKS
BE
CAREFUL
WITH DATES
SPLIT DATA
ON
MULTIPLECOLLECTIONS
friendly fire(aka RTFM)
Write
Concern
READ
PREFERENCE
ATOMIC
DOCUMENT
OPERATIONS
AVOID
NATURAL
KEYS ASIDENTIFIERS
Saturday, November 30,
![Page 18: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/18.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DESIGN
DESIGN
FOR
QUERYEMBEDDED
DATA
vsReferences
DBREFS
VS
MANUALREFERENCE
DYNAMIC
SCHEMA
VSstatic
languages
PURE DRIVER
VS
MAPPINGFRAMEWORKS
BE
CAREFUL
WITH DATES
SPLIT DATA
ON
MULTIPLECOLLECTIONS
friendly fire(aka RTFM)
Write
Concern
READ
PREFERENCE
ATOMIC
DOCUMENT
OPERATIONS
AVOID
NATURAL
KEYS ASIDENTIFIERS
PERFORMANCE
PREALLOCATE
FIELDS?
be aware
of
the trees
PREPROCESS
HIGH
RESOLUTIONDATA
TUNING
UPDATES
ANDINSERTS
DOCUMENT
MOVING
SLOWSYOU
Saturday, November 30,
![Page 19: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/19.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
FRIENDLY FIRE
Saturday, November 30,
![Page 20: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/20.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
ATOMIC
DOCUMENT
OPERATIONS
Saturday, November 30,
![Page 21: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/21.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
ATOMIC
DOCUMENT
OPERATIONS
OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC
Saturday, November 30,
![Page 22: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/22.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
ATOMIC
DOCUMENT
OPERATIONS
OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC
NO “ALL OR NOTHING”
Saturday, November 30,
![Page 23: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/23.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
ATOMIC
DOCUMENT
OPERATIONS
OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC
NO “ALL OR NOTHING”
EMBEDding OR APPLIcaTION TRANSACTIONS CAN be used to handle the issue
Saturday, November 30,
![Page 24: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/24.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
ATOMIC
DOCUMENT
OPERATIONS
OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC
NO “ALL OR NOTHING”
EMBEDding OR APPLIcaTION TRANSACTIONS CAN be used to handle the issue
RELATIONAL TRANSACTIONS ARE NOT TOTALLY SAFE
Saturday, November 30,
![Page 25: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/25.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
Saturday, November 30,
![Page 26: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/26.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 27: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/27.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Errors Ignored
Saturday, November 30,
![Page 28: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/28.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Errors Ignored Unacknowledged
Saturday, November 30,
![Page 29: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/29.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Errors Ignored Unacknowledged
Acknowledged (*)
Saturday, November 30,
![Page 30: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/30.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Errors Ignored Unacknowledged
Acknowledged (*) Journaled
Saturday, November 30,
![Page 31: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/31.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“Describes the guarantee that
MongoDB provides when reporting on the success of a write
operation”
Write
Concern
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Errors Ignored Unacknowledged
Acknowledged (*) Journaled
Replica Acknowledged> 1 , majority , custom using tags
Saturday, November 30,
![Page 32: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/32.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
Saturday, November 30,
![Page 33: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/33.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 34: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/34.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
primary (*)
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 35: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/35.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
primary (*) primary Preferred
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 36: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/36.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
primary (*) primary Preferred
secondary
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 37: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/37.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
primary (*) primary Preferred
secondary secondary PREFERRED
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 38: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/38.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
“IT describes how MongoDB clients
route read operations to members
of a replica set”
Read
Preference
primary (*)
nearest
primary Preferred
secondary secondary PREFERRED
IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION
Saturday, November 30,
![Page 39: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/39.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
All collections have an index on the id field that exists by default. If ID IS NOT PROVIDED the driver or the mongod will create an _id field with an ObjectID value.
AVOID
NATURAL
KEYS ASIDENTIFIERS
Saturday, November 30,
![Page 40: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/40.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
All collections have an index on the id field that exists by default. If ID IS NOT PROVIDED the driver or the mongod will create an _id field with an ObjectID value.
AVOID
NATURAL
KEYS ASIDENTIFIERS
ADD AN UNIQUE INDEX ON THE NATURAL KEY, SOMETIMES THE APPLICATION REALM CAN EVOLVE IN AN UNEXPECTED WAY
Saturday, November 30,
![Page 41: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/41.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
All collections have an index on the id field that exists by default. If ID IS NOT PROVIDED the driver or the mongod will create an _id field with an ObjectID value.
AVOID
NATURAL
KEYS ASIDENTIFIERS
ADD AN UNIQUE INDEX ON THE NATURAL KEY, SOMETIMES THE APPLICATION REALM CAN EVOLVE IN AN UNEXPECTED WAY
REMEMBER THAT UNIQUE INDEXES FIELDS MUST BE PART OF THE SHARD KEY IF SHARDING IS ENABLED
Saturday, November 30,
![Page 42: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/42.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
DESIGN
Saturday, November 30,
![Page 43: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/43.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION
DESIGN
FOR
QUERY
Saturday, November 30,
![Page 44: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/44.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION
DESIGN
FOR
QUERY
REFERENCE OR EMBED DOCUMENTS,
“denormalized” is not always
a bad word
Saturday, November 30,
![Page 45: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/45.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION
DESIGN
FOR
QUERY
REFERENCE OR EMBED DOCUMENTS,
“denormalized” is not always
a bad word
your document design will affect what kind of OPERATIONS will be safe or not
Saturday, November 30,
![Page 46: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/46.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Embedded data models allow applications to store related pieces of information in the same database record
EMBEDDED
DATA
vsReferences
Saturday, November 30,
![Page 47: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/47.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Embedded data models allow applications to store related pieces of information in the same database record
EMBEDDED
DATA
vsReferences
USUALLY there is a “contains” relation
between the embedding and the embedded object
Saturday, November 30,
![Page 48: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/48.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Embedded data models allow applications to store related pieces of information in the same database record
EMBEDDED
DATA
vsReferences
The maximum BSON document size is 16 megabytes and embedding may lead to performance issues if not correctly used
USUALLY there is a “contains” relation
between the embedding and the embedded object
Saturday, November 30,
![Page 49: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/49.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Normalized data models describe relationships using references between documents
EMBEDDED
DATA
vsReferences
Saturday, November 30,
![Page 50: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/50.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Normalized data models describe relationships using references between documents
EMBEDDED
DATA
vsReferences
References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries
Saturday, November 30,
![Page 51: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/51.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Normalized data models describe relationships using references between documents
EMBEDDED
DATA
vsReferences
NO Referential integrity is supported, references could point to a not existing object
References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries
Saturday, November 30,
![Page 52: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/52.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name
DBREFS
VS
MANUALREFERENCE
Saturday, November 30,
![Page 53: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/53.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name
DBREFS
VS
MANUALREFERENCE
MANUAL REFERENCES are just fields that will hold the id of the related document, without the collection name or the db name
Saturday, November 30,
![Page 54: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/54.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name
DBREFS
VS
MANUALREFERENCE
MANUAL REFERENCES are just fields that will hold the id of the related document, without the collection name or the db name
MANUAL REFERENCES are suitable for most of the use cases
Saturday, November 30,
![Page 55: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/55.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
BE
CAREFUL
WITH DATES
ALWAYS Use bson date when is related to an instant of time or you will never be able to use operators on that fields
Saturday, November 30,
![Page 56: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/56.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
BSON Date is a 64-bit signed integer that represents the number of milliseconds since the Unix epoch (Jan 1, 1970), Negative values represent dates before 1970.The official BSON specification refers to the BSON Date type as the UTC datetime.
BE
CAREFUL
WITH DATES
ALWAYS Use bson date when is related to an instant of time or you will never be able to use operators on that fields
Saturday, November 30,
![Page 57: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/57.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
split data on multiple collections to easily partition your data (a.k.a. Multitenancy)
SPLIT DATA
ON
MULTIPLECOLLECTIONS
Saturday, November 30,
![Page 58: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/58.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
split data on multiple collections to easily partition your data (a.k.a. Multitenancy)
SPLIT DATA
ON
MULTIPLECOLLECTIONS
use collections as namespaces for your data
Saturday, November 30,
![Page 59: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/59.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
split data on multiple collections to easily partition your data (a.k.a. Multitenancy)
SPLIT DATA
ON
MULTIPLECOLLECTIONS
use collections as namespaces for your data
remember once data is partioned it will be more hard to aggregate if needed
Saturday, November 30,
![Page 60: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/60.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
why use dynamic schema if we are not using a dynamic programming language?
DYNAMIC
SCHEMA
VSstatic
languages
Saturday, November 30,
![Page 61: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/61.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
why use dynamic schema if we are not using a dynamic programming language?
DYNAMIC
SCHEMA
VSstatic
languages
inheritance is not only a matter of hierarchy, it could be also a matter of composition
Saturday, November 30,
![Page 62: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/62.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
why use dynamic schema if we are not using a dynamic programming language?
DYNAMIC
SCHEMA
VSstatic
languages
inheritance is not only a matter of hierarchy, it could be also a matter of composition
composition is the key to introduce dynamic schema in a static programming language
Saturday, November 30,
![Page 63: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/63.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code
PURE DRIVER
VS
MAPPINGFRAMEWORKS
Saturday, November 30,
![Page 64: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/64.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code
PURE DRIVER
VS
MAPPINGFRAMEWORKS
MAPPING FRAMEWORKS WILL HELP TO WRITE LESS CODE, but you will sacrifice the control on all the aspects of the persistence
Saturday, November 30,
![Page 65: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/65.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code
PURE DRIVER
VS
MAPPINGFRAMEWORKS
MAPPING FRAMEWORKS WILL HELP TO WRITE LESS CODE, but you will sacrifice the control on all the aspects of the persistence
why not take the most from both?
Saturday, November 30,
![Page 66: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/66.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
PERFORMANCE
Saturday, November 30,
![Page 67: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/67.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Saturday, November 30,
![Page 68: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/68.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Saturday, November 30,
![Page 69: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/69.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field
Saturday, November 30,
![Page 70: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/70.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Saturday, November 30,
![Page 71: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/71.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Multikey
Saturday, November 30,
![Page 72: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/72.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Multikey Geospatial
Saturday, November 30,
![Page 73: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/73.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Multikey Geospatial
TEXT (BETA)
Saturday, November 30,
![Page 74: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/74.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Multikey Geospatial
TEXT (BETA) Hashed
Saturday, November 30,
![Page 75: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/75.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document
be aware
of
the trees
Indexes are created using a b-tree and can be of different types
Single Field Compound
Multikey Geospatial
TEXT (BETA) Hashed
THEY COULD BE UNIQUE and sparse
Saturday, November 30,
![Page 76: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/76.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR
DOCUMENT
MOVING
SLOWSYOU
Saturday, November 30,
![Page 77: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/77.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR
DOCUMENT
MOVING
SLOWSYOU
WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED
Saturday, November 30,
![Page 78: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/78.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR
DOCUMENT
MOVING
SLOWSYOU
WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED
DYNAMIC SCHEMA IS THE FIRST CAUSE OF DOCUMENT MOVING
Saturday, November 30,
![Page 79: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/79.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES
PREALLOCATE
FIELDS?
Saturday, November 30,
![Page 80: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/80.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES
PREALLOCATE
FIELDS?
Default values must be used to preallocate, this MUST BE HANDLEDin the application
Saturday, November 30,
![Page 81: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/81.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES
PREALLOCATE
FIELDS?
Default values must be used to preallocate, this MUST BE HANDLEDin the application
NULL is not a default value :-) as it has its own type
Saturday, November 30,
![Page 82: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/82.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB let you store the maximum resolution of your data
PREPROCESS
HIGH
RESOLUTIONDATA
Saturday, November 30,
![Page 83: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/83.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB let you store the maximum resolution of your data
PREPROCESS
HIGH
RESOLUTIONDATA
MAP REDUCE and aggregation ARE okbut you could also preprocess and have aggregated data that you can use for your queries
Saturday, November 30,
![Page 84: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/84.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MONGODB let you store the maximum resolution of your data
PREPROCESS
HIGH
RESOLUTIONDATA
MAP REDUCE and aggregation ARE okbut you could also preprocess and have aggregated data that you can use for your queries
MONGODB rocks for business intelligence
Saturday, November 30,
![Page 85: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/85.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table
TUNING
UPDATES
ANDINSERTS
Saturday, November 30,
![Page 86: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/86.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table
TUNING
UPDATES
ANDINSERTS
WRITING THE FIRST FIELD OF A DOCUMENT (OR A NESTED DOCUMENT) is considerably faster than writing THE LAST
Saturday, November 30,
![Page 87: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/87.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Alessandro Palumbo - [email protected] - http://www.byte-code.com
MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table
TUNING
UPDATES
ANDINSERTS
WRITING THE FIRST FIELD OF A DOCUMENT (OR A NESTED DOCUMENT) is considerably faster than writing THE LAST
Intra-Document Hierarchy could help to handle the issue
Saturday, November 30,
![Page 88: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/88.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Any questions?
Saturday, November 30,
![Page 89: Application Design per MongoDB](https://reader037.vdocument.in/reader037/viewer/2022103110/54772285b4af9f05438b4713/html5/thumbnails/89.jpg)
Except where otherwise noted, this work is licensed under: http://creativecommons.org/licenses/by/3.0/
Any questions?
Saturday, November 30,