tadasajon
tadasajon

Reputation: 14836

Meteor - subscribe to same collection twice - keep results separate?

I have a situation in which I need to subscribe to the same collection twice. The two publish methods in my server-side code are as follows:

Meteor.publish("selected_full_mycollection", function (important_id_list) {
    check(important_id_list, Match.Any);  // should do better check
    // this will return the full doc, including a very long array it contains
    return MyCollection.find({
        important_id: {$in: important_id_list}
    });
});
Meteor.publish("all_brief_mycollection", function() {
    // this will return all documents, but only the id and first item in the array
    return MyCollection.find({}, {fields: {
        important_id: 1,
        very_long_array: {$slice: 1}
    }});
});

My problem is that I am not seeing the full documents on the client end after I subscribe to them. I think this is because they are being over-written by the method that publishes only the brief versions.

I don't want to clog up my client memory with long arrays when I don't need them, but I do want them available when I do need them.

The brief version is subscribed to on startup. The full version is subscribed to when the user visits a template that drills down for more insight.

How can I properly manage this situation?

Upvotes: 6

Views: 1475

Answers (2)

Crenshinibon
Crenshinibon

Reputation: 187

A third option might be publishing the long version to a different collection that exists for this purpose on the client only. You might want to check the "Advanced Pub/Sub" Chapter of Discover Meteor (last sub chapter).

Upvotes: 1

richsilv
richsilv

Reputation: 8013

TL/DR - skip to the third paragraph.

I'd speculate that this is because the publish function thinks that the very_long_array field has already been sent to the client, so it doesn't send it again. You'd have to fiddle around a bit to confirm this, but sending different data on the same field is bound to cause some problems.

In terms of subscribing on two collections, you're not supposed to be able to do this as the unique mongo collection name needs to be provided to the client and server-side collections object. In practice, you might be able to do something really hacky by making one client subscription a fake remote subscription via DDP and having it populate a totally separate Javascript object. However, this cannot be the best option.

This situation would be resolved by publishing your summary on something other than the same field. Unfortunately, you can't use transforms when returning cursors from a publish function (which would be the easiest way), but you have two options:

  1. Use the low-level publications API as detailed in this answer.
  2. Use collection hooks to populate another field (like very_long_array_summary) with the first item in the array whenever very_long_array changes and publish just the summary field in the former publication.

Upvotes: 3

Related Questions