Hey! A dumb question about m-n relations with Mong...
# mongodb
d
Hey! A dumb question about m-n relations with Mongo. The docs say that reference IDs must be present at both sides. But why is that necessary? E.g. imagine
Post.categories
relation. In inverse relation
Category.posts
would explode the
Category
entity, there would be hundreds of thousands of entries in it. Besides, it may eventually lead to inconsistent date if
Post.categories
wouldn’t match
Category.posts
for some reason. So is there a way to only store the relation information on a
Post
side and query things based only on it? An alternative would be to get rid of m-n relations entirely and query for such relations manually. Which would be a shame of course.
1
n
Hey Dmitri 👋 thanks a lot for raising this, I’ve forwarded this to our Engineering team and will get back to as soon as possible with more information 🙂
d
Thanks @nikolasburk! Basically here’s a better explanation why one-way embedding is sometimes preferred https://learnmongodbthehardway.com/schema/schemabasics/#one-way-embedding
The reason we choose to embed all the references to categories in the books is due to there being lot more books in the drama category than categories in a book. If one embeds the books in the category document it’s easy to foresee that one could break the 16MB max document size for certain broad categories.
🙏 1
💯 1
n
Ah thanks for the additional reference! I just checked with out Engineers and we definitely want to add more “patterns” for m:n relations in MongoDB in the future. We just started with the “Two Way Embedding” strategy since it seemed to be the most ubiquitous… It would however be super nice if you could create a GitHub issue for this and explain why the “One Way Embedding” can be more preferable at times.
d
I will! Absolutely! Prisma team rocks!
j
There should already be one for that...
👀 1
d
@janpio the issues have a bit weird title. In my case I need a two-way relation, just modelled on a single end in Mongo (aka “One Way Embedding”)
j
I agree, see my edit history of the title on https://github.com/prisma/prisma/issues/12725 already 😄
But do you think the actual description fits?
d
“Support one-way embedding for Mongo m-n relations”?
j
Yeah that is how you and me would describe it - for others that does not make sense as they do not know the "one-way embedding" terminology 😄
But if you think the actual content of the issue, not the title, describes what you want or need, then I can update the title to cover both.
d
@janpio well, this comment of yours sums it up pretty well https://github.com/prisma/prisma/issues/12552#issuecomment-1092126073
in other words, the issue as it is makes very little sense in its current shape
j
Yeah. I meant the first one - 12725 😄
That was the outcome of that, a bit confusing, discussion
d
j
And I updated the title to use the more clear term.
❤️ 1