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:
- Comment A : 1 like 0 dislikes
- Comment B : 0 like 0 dislike
- Comment C : 0 like 1 dislike
What I'm actually getting:
- Comment A : 1 like 0 dislikes
- Comment C : 0 like 1 dislike
- Comment B : 0 like 0 dislike
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,
}