Abdullah Dahmash
Abdullah Dahmash

Reputation: 535

Publish a collections and other collection's documents which have relation with any document in the first collection

The scenario is that I want to publish one whole collection and users' data (such as profile) who have relation with any document in the the first collection.
The problem is how can I publish that part of users collections?

Upvotes: 0

Views: 81

Answers (2)

Abdullah Dahmash
Abdullah Dahmash

Reputation: 535

After some research, I found reywood:publish-composite solved my problem completly.
Example:

Meteor.publishComposite('getItemsList', {
find: function() {
    return Items.find({});
},
children: [
    {
        find: function(item) {
            return Meteor.users.find(
                { _id: item.userId },);
        }
    }
]});

This will publish all the items documents with any user document that have a relation with it. ( Items.userId is mapped to Meteor.users._id )

Upvotes: 0

sdooo
sdooo

Reputation: 1881

Well, there are two ways, first is using package

https://atmospherejs.com/cottz/publish-with-relations

And second one - in publish function you can return multiple cursors, from docs

Meteor.publish("roomAndMessages", function (roomId) {
  check(roomId, String);
  return [
    Rooms.find({_id: roomId}, {fields: {secretInfo: 0}}),
    Messages.find({roomId: roomId})
  ];
});

Upvotes: 1

Related Questions