Extend schema validation to be able to enforce referential integrity between collections
Where a relational database uses 2 tables to store a 1:many "parent - child" relationship between entities, MongoDB mostly stores the child documents in an array file as part of the parent document. This automatically ensures referential integrity in that
- a child document cannot be inserted or updated to refer to a non-existent parent, and
- a parent document cannot be deleted such that it leaves "orphaned" child documents
However, there are situations where the number and/or size of the child documents makes embedding them all in their parent unworkable, due to the 16 megabyte document size limit if nothing else.
In those cases, the documents are stored in separate collections, and schema validation cannot enforce the integrity of the references between them.
Schema validation already permits rules to be specified as $expr expressions for rules based on multiple fields in the document being inserted/updated, like "totalCost = basePrice + taxAmount".
One way this enhancement could be implemented is by extending the use of $expr
to permit use of an aggregation pipeline that does a $lookup
to the other collection.