Unexpected Mongo.Cursor behaviour with {limit}


#1

Hi,
I ve got strange mongo issue when use limit. For example:

const Posts = new Mongo.Collection('posts');

// and lets say we have 1000 docs in the collection

const selector = {author: 'me'};
const sort = {createdAt: -1};
const limit = 40;

const count = Posts.find(selector, {sort, limit}).count();
// traces 1000, but limit is 40!

const length = Posts.find(selector, {sort, limit}).fetch().length;
// traces 40 as expected

// The most problematic thing

Meteor.publish('myPosts', function myPosts(limit = 40) {
  const selector = {author: 'me'};
  const sort = {createdAt: -1};

  const transformDoc = function (doc) {return doc;};

  Posts.find(selector, {sort, limit}).observe({
    added: (doc) => {
      this.added('posts', doc._id, transformDoc(doc));
    },
    changed: (doc) => {
      this.changed('posts', doc._id, transformDoc(doc));  
    },
  });

  return this.ready();
});

So my added callback is called 1000 times, instead of 40! Is it normal? May be I missed some notes of that kind of behaviour?


#2

If i remember correctly, count does not take into account the limit. Try to fetch the data and use length on it


#3

Yeah, may be you are right. I am not so interested in count but in observe. It is ridiculous that added called 1000 times instead of 40!