Canonical Many to Many Relationship in Docs?

AnnotatedJS, have you tried building an app with many to many relationship reywood:publish-composite? I’m hoping someone could share some code with me, because it doesn’t seem obvious to me from the documentation.

Okay, here’s how to use that stuff:

You have two choices - with or without publish-composite. Both look nearly the same on the client.

Say, we have Comments and each Comment has an Author

Without publish-composite:

On the server: Publish Comments and publish the Authors.
On the client: Subscribe to Comments and Authors. Query for all the comments. For each comment, query for the author.

With publish-composite:
On the server: Publish Comments along with the authors for those comments.
On the client: Subscribe to the composite Comment-Authors. Query for all the comments. For each comment, query for the author.

The only difference is what you’re subscribing to and what gets published - publish-composite will reduce the amount of data which gets shoved down the pipe. But building the relationship on the client between collections? You’ll still have to do that by hand.

And many-to-many will likely require two CompositeCollections. Though If I think about it, in that case you may as well simply publish both collections completely :slight_smile:

Thank you rhywden, much appreciated. I get the Comments and Authors problem, it’s relatively straight forward and well documented. But, for many to many, I think what I need to do is find someone who has actually done it and tested it successfully, because it looks much more complicated. Maybe I’ll try StackOverflow.

Thanks again everyone for trying to help this hopeless newbie. :wink:

Don’t forget that MongoDB can store arrays as a field. Many-to-many could look like this:

Users : [
    {
        id: 'Baz'
        name: 'Foo',
        likes_comment: ['comment_123', 'comment_456']
    },
    {
        id: 'Bar'
        name: 'Fubar',
        likes_comment: ['comment_456']
    }
]
Comments: [
    {
        id: 'comment_123',
        content: 'Omnia Gallia est divisa in partes tres',
        liked_by: ['Baz', 'Bar']
    },
    {
        id: 'comment_456',
        content: 'Carthaginem esse delendam!',
        liked_by: ['Bar']
    }
]

You don’t need an extra collection storing the foreign keys to map the relationship between entries. You now can use publish-composite twice if you need to limit the amount of data to send to the client.
Once for the Users and their Likes - and once for the Likes and their Users.

@arunoda might be able to give an update on how relevant the content is

1 Like

The content is valid for Meteor 1.3 since there’s no huge changes in Meteor after 1.3 (related to APIs)

1 Like

Retracted this comment. Have resolved the question I asked.

No, never had the use case for a many-to-many relationship with (a) huge numbers and (b) the need of being reactive.