Skip to content
Sacha Lifszyc edited this page May 19, 2014 · 3 revisions

law.$comment.score

A new field named score has been added with the sorting comments feature at this release. This allows to sort comments according to their score, being calculated as follows: comment.score = comment.upvotes - comment.downvotes (minus).

In order to sort your comments you need to run the following command at your MongoDB shell:

db.comments.find().forEach(function (c) {
  var positive = c.votes.filter( function (v) {
    return v.value == 'positive';
  }).length;
  var negative = c.votes.filter( function (v) {
    return v.value == 'negative';
  }).length;
  var score = positive - negative;
  db.comments.update( { _id: c._id }, { $set: { score: score } } );
});

After this, the score will be set to corresponding number for each comment. And subsequent upvotes and downvotes will modify the comment.score field to reflect this new score.

Clone this wiki locally