0

I am referring to the example presented here: docs.meteor.com/#publish_added and i have read the answer to: How does the messages-count example in Meteor docs work?

I want to track the number of entries in 3 separate collections. Storing the values in a collection called counts. Unlike the example, I dont need to filter the tables (by roomId).

on the server i have:

Meteor.publish("counts-all", function () {
var self = this;

var pcount = 0;
var fcount = 0;
var scount = 0;
var initializing = true;

// observeChanges only returns after the initial `added` callbacks
// have run. Until then, we don't want to send a lot of
// `self.changed()` messages - hence tracking the
// `initializing` state.
var phandle = Persons.find({}).observeChanges({
  added: function (id) {
    pcount++;
    if (!initializing)
      self.changed('counts', 'persons', {count: pcount});
      // self.changed('counts', 0, {count: pcount});
  },
  removed: function (id) {
    pcount--;
    self.changed('counts', 'persons', {count: pcount});
    // self.changed('counts', 0, {count: pcount});
  }
  // don't care about changed
});

var fhandle = Families.find({}).observeChanges({
  added: function (id) {
    fcount++;
    if (!initializing)
      self.changed('counts', 'families', {count: fcount});
      // self.changed('counts', 1, {count: fcount});
  },
  removed: function (id) {
    fcount--;
    self.changed('counts', 'families', {count: fcount});
    // self.changed('counts', 1, {count: fcount});
  }
  // don't care about changed
});

var shandle = Sources.find({}).observeChanges({
  added: function (id) {
    scount++;
    if (!initializing)
      self.changed('counts', 'sources', {count: scount});
      // self.changed('counts', 2, {count: scount});
  },
  removed: function (id) {
    scount--;
    self.changed('counts', 'sources', {count: scount});
    // self.changed('counts', 2, {count: scount});
  }
  // don't care about changed
});

// Instead, we'll send one `self.added()` message right after
// observeChanges has returned, and mark the subscription as
// ready.
console.log('publish counts: '+pcount+' '+fcount+' '+scount);
initializing = false;

self.added('counts', 'persons', {count: pcount});
self.added('counts', 'families', {count: fcount});
self.added('counts', 'sources', {count: scount});


self.ready();

// Stop observing the cursor when client unsubs.
// Stopping a subscription automatically takes
// care of sending the client any removed messages.
self.onStop(function () {
  phandle.stop();
  fhandle.stop();
  shandle.stop();
});
});

I can see from my logging that it does appear to calculate the total number of records for each collection: publish counts: 168 150 36

on the client side, I have:

Counts = new Meteor.Collection("counts");

Deps.autorun(function () {
console.log('inside autorun');
Meteor.subscribe("counts-all");
console.log(Counts.find({}));
});

the logging doesnt show the entries in the collection. How would I reference these items on the client? Counts.findOne({_id: 'persons'}).count

Also, there are differences between the site example and the response in the question I mentioned :

  • the use of Dep vs Tracker
  • different arguments for the added and removed hash functions
  • use of Mongo.collection vs Meteor.collection.

Guidance would be appreciated

Community
  • 1
  • 1
dreadstar
  • 13
  • 4

1 Answers1

0

You can access counts on client side in this way:

    Deps.autorun(function () {
        console.log('inside autorun');
        Meteor.subscribe("counts-all");
        console.log(Counts.find({_id:"persons"}).fetch());
        var persons     = Counts.findOne({_id:"persons"}) || {};
        var sources     = Counts.findOne({_id:"sources"}) || {} ;
        var families    = Counts.findOne({_id:"families"}) || {};
        console.log(persons.count, sources.count, families.count);
    });

Mongo.collection vs Meteor.collection and Deps -> Tracker changes were introduced in v0.9.1.

Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • thank you. is there any reason not to just use Persons.find().count(); i saw some examples that seemed to reference counts that way without the counts collection and observer. is that method not reactive to changes in the collection? – dreadstar Sep 23 '14 at 18:23
  • You save bandwidth with this approach as you don't send documents only counts. – Kuba Wyrobek Sep 24 '14 at 06:19