6

Started playing with Meteor recently and came across a problem: I have collection of records that are stored in MongoDB and represent tests and each test record has nested collection/array of test execution results. On front-end I'm trying to display list of tests ordered by number of successful or failed test executions. That number is not stored in DB and is result of calculation.

Can anyone suggest an idea how to implement that in Meteor? I haven't found a way to sort by calculated property and was looking for ideas how to make virtual/proxy collection that is based on real one and display/sort virtual but wasn't able to find anything yet.

Your help is much appreciated.

Kostya Marchenko
  • 811
  • 1
  • 8
  • 7

1 Answers1

0

In my experience with Meteor, what you're after is a collection/document transform. Some code, which I tried to make relevant to your context (not tested).

Tests = new Mongo.Collection('tests', {
    transform: function(test) {
        test.successCount = function(){
            return Results.find({ testId: test._id, success: true }).count();
        };

        test.failCount = function(){
            return Results.find({ testId: test._id, success: false }).count();
        };

        return test; //this line is mandatory, transform must return the document
    }
});

Depending on the scale and traffic of your application you will need to decide whether this read, which will occur every time a test document is requested is more or less expensive then writing to an explicit success & fail count on the test document itself.

pim
  • 12,019
  • 6
  • 66
  • 69