How to sort by settlement rule / virtual attribute in Meteor?

I recently started playing with Meteor and ran into a problem: I have a collection of records that are stored in MongoDB and represent tests, and each test record has a nested set / array of test results. On the external interface, I am trying to display a list of tests ordered by the number of successful or failed tests. This number is not stored in the database and is the result of the calculation.

Can anyone suggest an idea how to implement this in Meteor? I did not find a way to sort by the computed property and was looking for ideas on how to make a virtual / proxy server assembly based on the real one and display / sort the virtual ones, but so far nothing has been found.

Your help is greatly appreciated.

+7
source share
1 answer

In my experience with Meteor, what you need is collecting / converting a document . Some code that 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 it will be a read, which will occur every time the requested test document is more or less expensive, and then it is written into the obvious success and failure of the test document itself.

+1
source

All Articles