
Cross join in MongoDB
Relational database joins are, conceptually, a cartesian product followed by a filter (the join condition). Without that condition, you get a cross join that returns every possible combination. In MongoDB, you can model the same behavior at read time using $lookup , or at write time by embedding documents. Example Define two collections: one for clothing sizes and one for gender-specific fits: db . sizes . insertMany ([ { code : " XS " , neckCm : { min : 31 , max : 33 } }, { code : " S " , neckCm : { min : 34 , max : 36 } }, { code : " M " , neckCm : { min : 37 , max : 39 } }, { code : " L " , neckCm : { min : 40 , max : 42 } }, { code : " XL " , neckCm : { min : 43 , max : 46 } } ]); db . fits . insertMany ([ { code : " MEN " , description : " Straight cut, broader shoulders, narrower hips " }, { code : " WOMEN " , description : " Tapered waist, narrower shoulders, wider hips " } ]); Each collection stores independent characteristics, and every size applies to every fit. The goal is t
Continue reading on Dev.to JavaScript
Opens in a new tab

