Laravel-mongodb: How to create two-way belongsToMany relationship on only one collection

Created on 22 Jun 2018  路  6Comments  路  Source: jenssegers/laravel-mongodb

Situation:

I have two models: Document and Pages. There's a Many-to-Many relationship, because a single Page can belong to multiple Documents, and each Document has multiple Pages.

I'm migrating from another database, and I currently have to relationship defined on the Document such that each Document has a page_ids array. I've created the BelongsToMany relationship on both models. It works fine from Page, but I really need it to work from Document.

I don't want to have to create a migration that adds a document_ids array on the Page model, as that's a pretty complicated migration.

So, I'm wondering if there is a way to define the relationship on Document such that it will look for a page_ids property on the Document model, rather than a document_ids property on the Page model.

question

All 6 comments

Did you try a reverse i.e. hasMany relation instead of belongsToMany on Page. As you've defined pages_ids array on each document, the hasMany (might) go the reverse way scanning every document's pages_id for the array you're searching for.

This analogy might work the way you expect. Give it a shot. And in your explanantion about the relation, you've mentioned a Many to Many with a belongsTo and a hasMany structure. Apply the idea same way that having belongsTo on both.

This is a required feature. Having the foreign id array in both collections is unnecessary. And the biggest issue is if the mapping from a small set to a huge set, the array size of foreign id in the small collection will be huge and might cause performance issue as well as may hit the 16 MB max document size. Say for example in case of role to user mapping, if I have 10 roles and around 100 million users, then the role document may have the user_ids array having an average number of 10 million user ids. Will this be a good practice?

Ref: http://learnmongodbthehardway.com/schema/schemabasics/

Establish Relationship Balance

Establish the maximum size of N and the size of M. For example if N is a maximum of 3 categories for a book and M is a maximum of 500000 books in a category you should pick One Way Embedding. If N is a maximum of 3 and M is a maximum of 5 then Two Way Embedding might work well.

Did you ever find a good solution to this?

@PiranhaGeorge

I created a new relationship, a kind of inverse to BelongsToMany. It's not perfect. It has a few issues that crop up from time-to-time, but it seems to work well for us.

Here's the code: https://gist.github.com/goodevilgenius/c2b04851f8d8aaeeb83bd501c103e692

@goodevilgenius that works perfectly for us! Thanks so much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sebastiaanluca picture sebastiaanluca  路  3Comments

naveedyasin picture naveedyasin  路  3Comments

phuocduy1988 picture phuocduy1988  路  3Comments

Vasiliy-Bondarenko picture Vasiliy-Bondarenko  路  3Comments

BlakeGardner picture BlakeGardner  路  3Comments