POLYMORPHIC SCHEMA
OO languages allow functions to manipulate child classes as though they were their parent classes, calling methods that are defined in the parent but may have been overridden with different implementations in the child. This feature of OO languages is referred to as polymorphism.
Relational databases, with their focus on tables with a fixed schema and does not allow us to define a related set of schemas for a table so that we could store any object in our hierarchy in the same table (and retrieve it using the same mechanism).
The flexibility that MongoDB offers by not enforcing a particular schema for all documents in a collection provides several benefits to the application programmer over an RDBMS solution:
- Better mapping of object-oriented inheritance and polymorphism
- Simpler migrations between schemas with less application downtime
- Better support for semi-structured domain data Effectively using MongoDB requires recognizing when a polymorphic schema may benefit your application and not over-normalizing your schema by replicating the same data layout you might use for a relational database system.
TRANSACTION
Relational database schemas often rely on the existence of atomic multistatement transactions to ensure data consistency: either all of the statements in a group succeed, or all of the statements fail, moving the database from one self-consistent state to another. MongoDB’s document model and its atomic update operations enable an approach that maintains consistency where a relational database would use a transaction. We can use an approach known as compensation to mimic the transactional behavior of relational databases.
The patterns we use in MongoDB to mitigate the lack of atomic multidocument update operations include
1)document embedding and complex updates for basic operations
2) optimistic update with compensation available for when we really need a two-phase commit protocol.
When designing your application to use MongoDB, more than in relational databases, you must keep in mind which updates you need to be atomic and design your schema appropriately.