Meteor - subscribe to the same collection twice - save the results separately?

I have a situation where I need to subscribe to the same collection twice. The two publishing methods in my server 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 do not see complete documents on the client side after I signed up for them. I think this is because they are overwritten by a method that publishes only short versions.

I do not want to clog my client memory with long arrays when I do not need them, but I want them to be available when I need it.

The short version is signed at startup. The full version is signed when the user visits a template that will unfold for a deeper understanding.

How can I properly manage this situation?

+6
source share
2 answers

TL / DR - Go to the third paragraph.

I would suggest that this is because the publish function considers that the very_long_array field very_long_array already been sent to the client, so it does not send it again. You need to play a bit to confirm this, but sending different data in one field may cause some problems.

As for subscribing to two collections, you should not do this, since the unique name of the mango collection must be provided to the collection object on the client side and on the server side. In practice, you can do something really hacked by making one client subscription a fake remote subscription via DDP and filling it out completely separately with the Javascript Object. However, this may not be the best option.

This situation will be resolved by publishing your resume about something other than the same field. Unfortunately, you cannot use conversions when returning cursors from the publish function (which would be the easiest way), but you have two options:

+3
source

The third option can publish a long version for another collection, which exists for this purpose only on the client. You might want to check out the "Advanced Pub / Sub" section of Discover Meteor (last chapter).

+1
source

Source: https://habr.com/ru/post/978755/


All Articles