简体   繁体   中英

Node+Mongo Native: generating request with sort

I'm creating step-by-step request for MongoDB. Everything is OK, but sort option cannot be recognized by Mongo. What I'm doing wrong?

var request = {}
    request['show'] = 1;
    request['category'] = category;
    switch (sort) {
        case "new_first":
            request['sort'] = "[['time_added', -1]]";
            break;
        case "old_first":
            request['sort'] = "[['time_added', 1]]";
            break;
    }
collection.find(request, function(err, posts) {
....    

Thanks!

Found a solution:

sort_query = JSON.parse('{"sort":[["time_added",-1]]}')

It worked!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM