Reputation: 97
This is about likes and dislikes on comments. I'd like to sort comments by likeness, so a comment with more likes will appear first. The issue is that comments with dislikes appear before than comments with no dislikes.
What i want:
What I'm actually getting:
My mongo query on my meteor helper:
Missatges.find({ topic: 'xxx' }, { sort: { like:{ $size: -1}, dislike:{ $size: 1}, date: -1 } }).fetch();
Note that I use $size because like and dislike are arrays of user _id's. Here my comment schema:
author: {
type: String
},
authorId: {
type: SimpleSchema.RegEx.Id
}
isAnswer: {
type: Boolean
},
parentId: {
type: SimpleSchema.RegEx.Id,
optional: true
},
date: {
type: Date
},
topic: {
type: String
},
likes: {
type: [SimpleSchema.RegEx.Id]
},
dislikes: {
type: [SimpleSchema.RegEx.Id]
},
answers: {
type: [SimpleSchema.RegEx.Id],
optional: true
},
text: {
type: String,
max: 900,
}
Upvotes: 1
Views: 102
Reputation: 64312
According to the answer to this question you can't use $size
to sort in mongo. One solution is to fetch
the matching documents and sort
them in the helper. Here's an example (note this is untested and may require some tweaking):
Template.something.helpers({
sortedThings: function () {
return Missatges
.find({ topic: 'xxx' })
.fetch()
.sort(function (b, a) {
var al = a.likes.length;
var ad = a.dislikes.length;
var bl = a.likes.length;
var bd = a.dislikes.length;
var d1 = a.date;
var d2 = b.date;
// compare by likes
if (al > bl) return 1;
if (al < bl) return -1;
// compare by dislikes
if (ad < bd) return 1;
if (ad > bd) return -1;
// compare by date
if (d1 > d2) return 1;
if (d1 < d2) return -1;
// they are equal
return 0;
});
},
});
Upvotes: 1