Application Design per MongoDB

781 views

Published on

Quando si approccia MongoDB da sviluppatore, il rischio piú grande è quello di usare pattern e design legati al mondo SQL trascurando il vero cambiamento che MongoDB ci impone: cambiare il nostro modo di pensare.

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
781
On SlideShare
0
From Embeds
0
Number of Embeds
136
Actions
Shares
0
Downloads
110
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Application Design per MongoDB

  1. 1. Application Design FOR MongoDB Alessandro Palumbo apalumbo@byte-code.com http:/ /it.linkedin.com/in/alessandropalumbo/ http:/ /www.byte-code.com Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  2. 2. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  3. 3. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB NoSql Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  4. 4. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB NoSql OPEN-source Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  5. 5. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB NoSql OPEN-source Document-Oriented JSON-style documents Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  6. 6. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com MongoDB NoSql OPEN-source Document-Oriented JSON-style documents from humongous “huge; enormous” Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  7. 7. Alessandro Palumbo - apalumbo@byte-code.com - 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) } Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  8. 8. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  9. 9. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  10. 10. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins NO FULL transactions Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  11. 11. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins NO FULL transactions no SCHEMA Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  12. 12. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins WE CAN EMBED NO FULL transactions no SCHEMA Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  13. 13. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins WE CAN EMBED NO FULL transactions DOCUMENT LEVEL TRANSACTIONS no SCHEMA Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  14. 14. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com don’t be relationaL no joins WE CAN EMBED NO FULL transactions no SCHEMA DOCUMENT LEVEL TRANSACTIONS IS IT REALLY AN ISSUE? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  15. 15. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  16. 16. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com friendly fire (aka RTFM) ATOMIC DOCUMENT OPERATIONS Write Concern READ PREFERENCE AVOID NATURAL KEYS AS IDENTIFIERS Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  17. 17. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com friendly fire DESIGN (aka RTFM) ATOMIC DOCUMENT OPERATIONS Write Concern READ PREFERENCE AVOID NATURAL KEYS AS IDENTIFIERS DESIGN FOR QUERY DBREFS VS MANUAL REFERENCE DYNAMIC SCHEMA VS static languages EMBEDDED DATA vs References BE CAREFUL WITH DATES SPLIT DATA ON MULTIPLE COLLECTIONS PURE DRIVER VS MAPPING FRAMEWORKS Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  18. 18. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com friendly fire DESIGN PERFORMANCE (aka RTFM) ATOMIC DOCUMENT OPERATIONS Write Concern READ PREFERENCE AVOID NATURAL KEYS AS IDENTIFIERS DESIGN FOR QUERY DBREFS VS MANUAL REFERENCE DYNAMIC SCHEMA VS static languages EMBEDDED DATA vs References be aware of the trees BE CAREFUL WITH DATES PREALLOCATE FIELDS? SPLIT DATA ON MULTIPLE COLLECTIONS TUNING UPDATES AND INSERTS PURE DRIVER VS MAPPING FRAMEWORKS Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30, DOCUMENT MOVING SLOWS YOU PREPROCESS HIGH RESOLUTION DATA
  19. 19. FRIENDLY FIRE Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  20. 20. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com ATOMIC DOCUMENT OPERATIONS Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  21. 21. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com ATOMIC DOCUMENT OPERATIONS OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  22. 22. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com ATOMIC DOCUMENT OPERATIONS OPERATIONS ON MULTIPLE DOCUMENTS ARE NOT ATOMIC NO “ALL OR NOTHING” Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  23. 23. Alessandro Palumbo - apalumbo@byte-code.com - 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  24. 24. Alessandro Palumbo - apalumbo@byte-code.com - 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  25. 25. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  26. 26. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  27. 27. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Errors Ignored Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  28. 28. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Errors Ignored Unacknowledged Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  29. 29. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Errors Ignored Unacknowledged Acknowledged (*) Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  30. 30. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Errors Ignored Unacknowledged Acknowledged (*) Journaled Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  31. 31. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Write Concern “Describes the guarantee that MongoDB provides when reporting on the success of a write operation” 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  32. 32. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  33. 33. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  34. 34. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  35. 35. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) primary Preferred Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  36. 36. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) primary Preferred secondary Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  37. 37. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) primary Preferred secondary secondary PREFERRED Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  38. 38. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com Read Preference “IT describes how MongoDB clients route read operations to members of a replica set” IT IS SET BY THE CLIENT AND CAN BE SET FOR EACH OPERATION primary (*) primary Preferred secondary secondary PREFERRED nearest Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  39. 39. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com AVOID NATURAL KEYS AS IDENTIFIERS 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. Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  40. 40. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com AVOID NATURAL KEYS AS IDENTIFIERS 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. ADD AN UNIQUE INDEX ON THE NATURAL KEY, SOMETIMES THE APPLICATION REALM CAN EVOLVE IN AN UNEXPECTED WAY Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  41. 41. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com AVOID NATURAL KEYS AS IDENTIFIERS 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. 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  42. 42. DESIGN Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  43. 43. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DESIGN FOR QUERY DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  44. 44. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DESIGN FOR QUERY DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION REFERENCE OR EMBED DOCUMENTS, “denormalized” is not always a bad word Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  45. 45. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DESIGN FOR QUERY DOCUMENT DESIGN IS FUNCTIONAL TO THE QUERIES THAT WILL EXISTS IN THE APPLICATION 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  46. 46. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Embedded data models allow applications to store related pieces of information in the same database record Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  47. 47. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Embedded data models allow applications to store related pieces of information in the same database record USUALLY there is a “contains” relation between the embedding and the embedded object Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  48. 48. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Embedded data models allow applications to store related pieces of information in the same database record USUALLY there is a “contains” relation between the embedding and the embedded object The maximum BSON document size is 16 megabytes and embedding may lead to performance issues if not correctly used Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  49. 49. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Normalized data models describe relationships using references between documents Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  50. 50. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Normalized data models describe relationships using references between documents References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  51. 51. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com EMBEDDED DATA vs References Normalized data models describe relationships using references between documents References provides more flexibility than embedding but remember that client-side applications will have to lookup for referenced objects with multiple queries NO Referential integrity is supported, references could point to a not existing object Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  52. 52. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DBREFS VS MANUAL REFERENCE DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  53. 53. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DBREFS VS MANUAL REFERENCE DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name MANUAL REFERENCES are just fields that will hold the id of the related document, without the collection name or the db name Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  54. 54. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DBREFS VS MANUAL REFERENCE DBRefs are a convention for representing a document, it will hold the collection name, the id, and optionally the db name 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  55. 55. Alessandro Palumbo - apalumbo@byte-code.com - 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  56. 56. Alessandro Palumbo - apalumbo@byte-code.com - 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 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. Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  57. 57. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com SPLIT DATA ON MULTIPLE COLLECTIONS split data on multiple collections to easily partition your data (a.k.a. Multitenancy) Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  58. 58. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com SPLIT DATA ON MULTIPLE COLLECTIONS split data on multiple collections to easily partition your data (a.k.a. Multitenancy) use collections as namespaces for your data Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  59. 59. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com SPLIT DATA ON MULTIPLE COLLECTIONS split data on multiple collections to easily partition your data (a.k.a. Multitenancy) use collections as namespaces for your data remember once data is partioned it will be more hard to aggregate if needed Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  60. 60. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DYNAMIC SCHEMA VS static languages why use dynamic schema if we are not using a dynamic programming language? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  61. 61. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DYNAMIC SCHEMA VS static languages why use dynamic schema if we are not using a dynamic programming language? inheritance is not only a matter of hierarchy, it could be also a matter of composition Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  62. 62. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DYNAMIC SCHEMA VS static languages why use dynamic schema if we are not using a dynamic programming language? 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  63. 63. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PURE DRIVER VS MAPPING FRAMEWORKS using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  64. 64. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PURE DRIVER VS MAPPING FRAMEWORKS using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code MAPPING FRAMEWORKS WILL HELP TO WRITE LESS CODE, but you will sacrifice the control on all the aspects of the persistence Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  65. 65. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PURE DRIVER VS MAPPING FRAMEWORKS using the mongo driver directly will give you great powers, but will force you to write a lot of boilerplate code 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? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  66. 66. PERFORMANCE Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  67. 67. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  68. 68. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  69. 69. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  70. 70. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  71. 71. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Multikey Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  72. 72. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Multikey Geospatial Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  73. 73. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Multikey Geospatial TEXT (BETA) Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  74. 74. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document Indexes are created using a b-tree and can be of different types Single Field Compound Multikey Geospatial TEXT (BETA) Hashed Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  75. 75. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com be aware of the trees Indexes in MongoDB are defined at the collection level and can be on any field or sub-field of the document 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  76. 76. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DOCUMENT MOVING SLOWS YOU MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  77. 77. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DOCUMENT MOVING SLOWS YOU MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  78. 78. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com DOCUMENT MOVING SLOWS YOU MONGODB handle the space allocation of a RECORD considering also a PADDING FACTOR WHEN AN UPDATED DOCUMENT DOES NOT FIT IN THE RECORD SPACE IT WILL BE MOVED DYNAMIC SCHEMA IS THE FIRST CAUSE OF DOCUMENT MOVING Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  79. 79. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREALLOCATE FIELDS? FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  80. 80. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREALLOCATE FIELDS? FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES Default values must be used to preallocate, this MUST BE HANDLEDin the application Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  81. 81. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREALLOCATE FIELDS? FIELDS PREALLOCATION CAN FIX THE DOCUMENT MOVING ISSUES IN SOME USE CASES 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  82. 82. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREPROCESS HIGH RESOLUTION DATA MONGODB let you store the maximum resolution of your data Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  83. 83. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREPROCESS HIGH RESOLUTION DATA MONGODB let you store the maximum resolution of your data MAP REDUCE and aggregation ARE ok but you could also preprocess and have aggregated data that you can use for your queries Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  84. 84. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com PREPROCESS HIGH RESOLUTION DATA MONGODB let you store the maximum resolution of your data MAP REDUCE and aggregation ARE ok but you could also preprocess and have aggregated data that you can use for your queries MONGODB rocks for business intelligence Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  85. 85. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com TUNING UPDATES AND INSERTS MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  86. 86. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com TUNING UPDATES AND INSERTS MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table WRITING THE FIRST FIELD OF A DOCUMENT (OR A NESTED DOCUMENT) is considerably faster than writing THE LAST Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  87. 87. Alessandro Palumbo - apalumbo@byte-code.com - http:/ /www.byte-code.com TUNING UPDATES AND INSERTS MongoDB stores BSON documents as a sequence of fields and values, not as aN hash table 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 Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  88. 88. Any questions? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,
  89. 89. Any questions? Except where otherwise noted, this work is licensed under: http:/ /creativecommons.org/licenses/by/3.0/ Saturday, November 30,

×