Publish multiple collections in one Meteor.publish call

2,901 views
Skip to first unread message

Michael Layzell

unread,
Jun 9, 2013, 11:50:00 PM6/9/13
to meteo...@googlegroups.com
When loading pages in my Meteor app, I have an overarching collection.  An item (`parent`) is retrieved from this collection by a non-_id property of the object.
I also then, at the same time as loading this item from the collection, need to retrieve a series of items from other collections, searching through these collections for a property equaling the _id of the `parent` object.

Currently I use a series of Deps calls to make a series of different Meteor.subscribe calls, first to get the `parent` item, and then, once that request to the server has been made, to get the child items.  This works OK, except that it means that two distinct batches of Meteor.subscribe calls have to be made at different times in the execution of the program.  

I would prefer it if the Meteor.publish call on the server could simply publish elements from both collections at the same time, as the _id for the `parent` object will be available on the server.
Making the child objects a property of the parent object is not an option.
Is there a way for Meteor.publish to publish items from/to more than one collection?

Erick Cardenas-Mendez

unread,
Jun 10, 2013, 12:25:19 AM6/10/13
to meteo...@googlegroups.com
Look at the 2 examples for Meteor.publish. In particular, the second example publishes a collection "counts" that doesn't exist on the Mongo DB, but is constructed as changes are done to the Messages collection.


Erick Cardenas-Mendez
www.playlistparty.net (built with Meteor)



--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Layzell

unread,
Jun 10, 2013, 12:27:19 AM6/10/13
to meteo...@googlegroups.com
I saw that, could I publish two distinct collections on the client using that method?  Could I for example publish both "counts" and "messages" to the client in the same publish call?  Or do you always need 2 publish calls to publish two collections on the client?

You received this message because you are subscribed to a topic in the Google Groups "meteor-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/meteor-talk/IEjaWgNocZg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to meteor-talk...@googlegroups.com.

Erick Cardenas-Mendez

unread,
Jun 10, 2013, 12:32:34 AM6/10/13
to meteo...@googlegroups.com
You could definitely publish from 2 collections. If you look at that 2nd example, the document being sent to the client with self.changed() or self.added() is the object {count: count}. But that document could really hold any fields, and the values for those fields could definitely be coming from two different MongoDB collection.



Erick Cardenas-Mendez
www.playlistparty.net (built with Meteor)



Michael Layzell

unread,
Jun 10, 2013, 12:40:36 AM6/10/13
to meteo...@googlegroups.com
I would just like it to appear as two separate collections on the client, rather than appearing in one aggregate collection on the client.  I simply don't know whether it is possible to publish to two separate collections on the client.

example of idea:

if (Meteor.isServer) {
  Meteor.publish('name', function(slug) {
    var parents = Parents.find({slug: slug});
    var parent_id = parents.fetch()[0]._id;
    var children = Children.find({parent: parent_id});

    return COMBINED(parents, children);
  });
} else {
  Meteor.subscribe('name', 'SLUG');

  // … LATER …
  
  var parent = Parents.findOne({slug: 'SLUG'});
  var children = Children.find({parent: parent._id});
}

Is that possible?  

Erick Cardenas-Mendez

unread,
Jun 10, 2013, 12:54:26 AM6/10/13
to meteo...@googlegroups.com
oh, okay, I thought you DID want to aggregate them in one collection.

Well, the first example in that section does show that you can return an array of Collection cursors, but I've never done that and I honestly don't know what the client-side of that looks like BUT if I had to guess, it would be something like...

// Client AND Server

Rooms = new Meteor.Collection("chatrooms");
Messages = new Meteor.Collection("messages");

// Client only

Meteor.subscribe("roomAndMessages", roomId);

// Server only

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


Erick Cardenas-Mendez
www.playlistparty.net (built with Meteor)



Michael Layzell

unread,
Jun 10, 2013, 1:11:06 AM6/10/13
to meteo...@googlegroups.com
Oh, I completely missed that.

Thank you very much, if that works it looks like it would fix all of my problems :).  That is what I get for only skimming the examples.  

Daniel Dornhardt - Daniel Dornhardt Development

unread,
Jun 10, 2013, 3:13:27 AM6/10/13
to meteo...@googlegroups.com
Wow, that's actually pretty great, I had the same issue and that might just be the solution for me too.

I also read across the "returning an array..." - Feature of the publish method, but I never connected the dots when I later had the problem.

Thanks!

Daniel Dornhardt, Daniel Dornhardt Development
+49 152 - 56 17 22 61
Reply all
Reply to author
Forward
0 new messages