2

Is there a way to store subscriptions of the same server collection in a different minimongo collection?

If not is there any best practice to work around?

I do have a summary table having 50k datasets with a lot of details in the documents.

// Server
var collection = new Meteor.Collection("collection");
Meteor.publish("detail", function (id) {
   return collection.find({_id: id});
});
// A pager that does not include the data (fields:{data:0})
Meteor.publish("master", function (filter, sort, skip, limit) {
   return collection.find({name: new RegExp("^" + filter + "|\\s" + filter, "i")}, {limit: limit, skip: skip, sort: options, fields: {data: 0}});
});

// Client
var collection = new Meteor.Collection("collection");
Deps.autorun(function () {
  Meteor.subscribe("master",
      Session.get("search"),
      Session.get("sort"),
      Session.get("skip"),
      Session.get("limit")
  );
  Meteor.subscribe("detail", Session.get("selection"));
});

Problem above: both subscriptions are feed into the same collection.

This does not work well if the results of the finds are stored in the same local collection.

Having a local collection with the name of the subscription/publish would be great.

// Client
var detail = new Meteor.Collection("detail"),
    master = new Meteor.Collection("master");

Any Ideas?

nomeasmo
  • 51
  • 5
  • If I create a collection with the subscription name it is always empty (obviously/unfortunately) Creating more than one collection having the same name is also not possible. It does not appear that I can pass a collection to a subscriptions. – nomeasmo Jun 17 '13 at 15:15
  • you can create unmanaged collections just create a collection on the client and do not give it a name – Micha Roon Jun 17 '13 at 20:09
  • Dr Gorb/Don please take a look at the example. If I could use unmanaged collections I need a hint. – nomeasmo Jun 18 '13 at 11:59

1 Answers1

3

If you want your client side collection to have a different name from the server side collection you can not just return a collection cursor. This can be done in the publish function though like this:

Meteor.publish("details", function (id) {  //details here matches the subscribe request
  var self = this;

  self.added( "details", id, collection.findOne({_id: id});  //details here tells the client which collection holds the data
  self.ready();
});

This will not be reactive but can be made that way by using observe as in the counts by room example at http://docs.meteor.com which is explained in detail here How does the messages-count example in Meteor docs work?.

While this answers your question of how to get a specific name for a collection without having that collection on the server. I think you probably get what you want more easily with a publish function more like this:

Meteor.publish("master", function (filter, sort, skip, limit, id) {

  return [ 
    collection.find({name: new RegExp("^" + filter + "|\\s" + filter,     "i")}, {limit: limit, skip: skip, sort: options, fields: {data: 0}})
    , collection.find( id , {fields: {data: 1}} )
    ];
});

Then subscribe on client:

Deps.autorun(function () {
  Meteor.subscribe("master",
    Session.get("search"),
    Session.get("sort"),
    Session.get("skip"),
    Session.get("limit"),
    Session.get("selection")
  );
});

Then even though all your data is in one collection you can have a reactive cursor to your selected id with the data included. Query from the client like this:

collection.find( Session.get("selection") );
Community
  • 1
  • 1
user728291
  • 4,138
  • 1
  • 23
  • 26