Why does cursor.observe invoke its added callback although there have been no intermediate changes?


I have just started to use cursor.observe to react to a collection changing. However, I am experiencing that immediately after registering observe callbacks on the collection’s find cursor, the added callback gets invoked for every query result. Why is this? Intuitively at least, I would think that the added callback would only get called when documents are added to the collection after my initial query (this is also what I want).

Example code:

projects = Projects.find({}, {sort: [["created", "asc"]]})
    added: (project) ->
      logger.debug("A project (#{getQualifiedId(project)}) was added, updating Isotope grid")
    changed: (project) ->
      logger.debug("A project (#{getQualifiedId(project)}) was changed, updating Isotope grid")
    removed: (project) ->
      logger.debug("A project (#{getQualifiedId(project)}) was removed, updating Isotope grid")


Before observe returns, added (or addedAt) will be called zero or more times to deliver the initial results of the query.

Source: http://docs.meteor.com/#/full/observe

Don’t know why it was designed this way but I’m guessing it was simpler to use the passed function or since the initial results are technically being ‘added’ to the publication.

To check if the observe has initialised, you can do something like the following:

Meteor.publish('ddpPub', function(filter) {
  var self = this,
      ready = false,
      count = 0;

  var subHandle = TestData.find(filter || {}).observeChanges({
    added: function (id, fields) {
      if (!ready) {
        count ++;
      self.added("testdata", id, fields);
    changed: function(id, fields) {
      self.changed("testdata", id, fields);
    removed: function (id) {
      self.removed("testdata", id);

  self.added("collectioncount", Random.id(), {Collection: "testdata", Count: count});

  ready = true;

More info: http://richsilv.github.io/meteor/meteor-low-level-publications/ (Pattern 1)


Ah thank you, didn’t notice this myself.