3

I have been writing a small node application and part of the query is sorting by the number of likes an object has, like this :

likes      : { num: { type: Number, default: 0 }, IDs:[Number]...

and the sort query:

Object.find().sort({likes : 'desc'};).execFind(function(err, confessions){...

this doesnt sort by the number of likes (like I would like it to) instead it sorts by the most recently liked object.

How would I sort by likes.num? I cant seem to get it to work...

EDIT: Heres a mongo doc as requested

{ __v: 0,
    _id: 51703a470ef8e60000000002,
    name: 'Name',
    text: ' this is a fresh submission for  mongo to hold',
    comments: { commentData: [], num: 0 },
    removed: false,
    time: Thu Apr 18 2013 11:24:07 GMT-0700 (PDT),
    likes: { IDs: [ 100001810858894 ], num: 1 },
    dislikes: { IDs: [ 100001810858894 ], num: 1 } }

EDIT: Heres the whole query

exports.index = function (req, res) {
console.log(req.user);
var sortParams;
var sort = req.params.sortingMethod;
console.log(sort);
if (sort == 'newest'){
     sortParams = { time : 'asc'};
} else if (sort == 'oldest'){
     sortParams = { time : 'desc'};
} else if (sort == 'likes'){
     sortParams = {likes.num : 'desc'};
} else if (sort == 'dislikes'){
     sortParams = {dislikes.num :'desc'};
} else {
     sortParams = { time : 'desc'};
}

console.log(sortParams);

Confession.find().sort(sortParams).execFind(function(err, confessions){...
4
  • you need to sort by the actual number of likes - where is it stored in the document? Can you paste an actual document from mongo into your question? Commented Apr 18, 2013 at 23:09
  • what does the likes document represent? is num going to be the number of likes? Then you need to sort on "likes.num" Commented Apr 18, 2013 at 23:14
  • This breaks the entire app with an Unexpected token . error, I had tried this before with the same result. Also yes num is the number of likes Commented Apr 18, 2013 at 23:17
  • 2
    Could you try with sortParams = {'likes.num' : 'desc'}; ? Commented Apr 19, 2013 at 0:38

2 Answers 2

6

The answer was to wrap the query parameters in quotes like Linda Quin suggested

sortParams = {'likes.num' : 'desc'};
Sign up to request clarification or add additional context in comments.

2 Comments

and like I said in the original answer and in its comment.
Oh, im sorry, I now realize that you ment for me to include the quotes. Thank you very much for your help
0

You are getting the results sorted by object's value - the problem is that the object is the full JSON document. What you actually want is sorting by value of the specific field within the object, in your case that would be "likes.num"

The quotes are to be included in the query:

 {"likes.num":-1} 

2 Comments

This breaks the entire app with an Unexpected token . error, I had tried this before with the same result. Also yes num is the number of likes. sortParams = {likes.num : 'desc'}; SyntaxError: Unexpected token .
You need to use double quotes around dot expressions. {"likes.num":-1}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.