<commit_before>Meteor.publish('Jobs', (jobId) =>
jobId ? Jobs.find({jobId: jobId}) : Jobs.find()
)

Meteor.publish('JobsResults', (jobId, limit) =>
JobsResults.find({jobId:jobId}, {limit: limit, sort: {createdAt:-1}})
)

Meteor.publish('JobsResult', (jobId, resultId) =>
JobsResults.find({jobId:jobId, resultId: resultId})
)

Meteor.publish('jobsResultDistinct', function(jobId) {

  let results = JobsResults.aggregate(
    [
      {
        $match: {
          'jobId': jobId,
          'data': {$exists: true}
        }
      },
      {
        $group: {
          _id: { hashedResult: "$hashedResult", data: "$data" },
          count: { "$sum": 1 }
        }
      }
    ]
  )

  var r = this;
  _.each(results, function(result) {
    r.added('JobsResults', Random.id(), {_r:result});
  });

  r.ready();<commit_msg>Add limitation to aggregated query results<commit_after>Meteor.publish('Jobs', (jobId) =>
  jobId ? Jobs.find({_id: jobId}) : Jobs.find()
)

Meteor.publish('JobsResults', (jobId, limit) =>
JobsResults.find({jobId:jobId}, {limit: limit, sort: {createdAt:-1}})
)

Meteor.publish('JobsResult', (jobId, resultId) =>
JobsResults.find({jobId:jobId, resultId: resultId})
)

Meteor.publish('jobsResultDistinct', function(jobId, limit) {

  let aggregation = [
    {
      $match: {
        'jobId': jobId,
        'data': {$exists: true}
      }
    },
    {
      $group: {
        _id: { hashedResult: "$hashedResult", data: "$data" },
        count: { "$sum": 1 }
      }
    }
  ]

  if (!!limit) {
    aggregation.push({
      $limit: limit
    })
  }

  let results = JobsResults.aggregate(aggregation)

  var r = this;
  _.each(results, function(result) {
    r.added('JobsResults', Random.id(), {_r:result});
  });

  r.ready();
{'fraction_non_alphanumeric': 0.18810748999428245, 'fraction_numerical': 0.002287021154945683, 'mean_word_length': 2.9592760180995477, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5171000', 'n_tokens_mistral': 657, 'n_tokens_neox': 626, 'n_words': 110}