I designed an entity relationship diagram (ERD) with the goal of creating two child entities (INSTITUTION
and SPACECRAFT
) of a parent entity (LOCATION
), which is distinguished depending on the value of a boolean attribute (IS_SPACECRAFT
). Implementing this in MongoDB, I'd like to link the INSTITUTION
entity to another separate entity when IS_SPACECRAFT
is false (0), and the SPACECRAFT
entity to this other separate entity when IS_SPACECRAFT
is true (1).
Right now, I am doing this manually by creating two attributes (SPACECRAFT_ID
and INSTITUTION_ID
) in this unrelated entity and pasting the object ID of either the INSTITUTION
or SPACECRAFT
entity in this attribute depending on the value of IS_SPACRECRAFT
. But this inevitably leads to one of those attributes being NULL, which I think is poor database design and is something I'd like to avoid. With that in mind, is there a way to link another entity (i.e., as a foreign key) depending on a Boolean value in MongoDB?
Attached below is the ERD I mentioned earlier so the hierarchy is clear. The discriminator (d) is meant to represent a disjoint (overlapping) subtype, and the double lines represent a total completeness constraint, such that all the member of the parent LOCATION
entity are in either SPACECRAFT
or LOCATION
, but not both.
For reference, a disjoint subtype is defined as:
Disjoint subtypes, also known as nonoverlapping subtypes, are subtypes that contain a unique subset of the supertype entity set; in other words, each entity instance of the supertype can appear in only one of the subtypes.
https://www.google.com/books/edition/Database_Systems_Design_Implementation_M/4JN4CgAAQBAJ