1

Let's say that I have two Meteor collections: "Stations" and "Songs". Each song has a foreign key to a station to show that it was played on that station.

Now I want to publish the last 3 played songs of every station like so:

Meteor.publish("all-last-three-songs", function () {
    var stations = Stations.find();
    var stationIDs = stations.map(function(station) { return station._id });
    var songs = Songs.find({station_id: {$in: stationIDs}}, {sort: {time: -1}, {limit: 3}});
    return songs;
});

That publishes only 3 songs for all stations of course. How can I achieve that a cursor for the last 3 songs for every station gets returned? I don't want to do the filtering on the client side.

Thank you! :)

user1259201
  • 706
  • 7
  • 21

1 Answers1

0

You could return an array of cursors by using the map function. Just stick your song query in your current map function. Instead of returning an id it would return the last three songs for the station with the given id.

You could also convert this into an array of arrays or flatten it into a single array.

You could then do a new query on the database for all songs whose id is in this array.

Eliezer Steinbock
  • 4,728
  • 5
  • 31
  • 43
  • Thank you! Two things here:As the documentation states the cursors need to come from different collections, thus making it impossible to solve it that way in my case. Returning an array isnt quite "meteor-style", is it? I don't think I can get that cool full stack reactivity with that. – user1259201 Nov 26 '14 at 09:25