dev dev
dev dev

Reputation: 123

sorting not working in mongodb

my script :

function getAllExperiences(){
    return new Promise(function(resolve,reject){
        var perPage = 10
        var page = req.params.page || 1
        var type = req.body.type;
        var sortAs = req.body.sortAs;
        Experiences
            .find({status:'live'})
            .sort({type:sortAs})//,{duration:durationValue}
            .skip((perPage * page) - perPage)
            .limit(perPage)
            .exec(function(err, results) {
                Experiences.count().exec(function(err, count) {
                    if (err)
                    {
                        reject(err);
                    }else{
                        var obj = 
                        {
                            experiences: results,
                            current: page,
                            pages: Math.ceil(count / perPage)
                        };
                        resolve(obj);
                    }
                })
            })
    });
}

i am sorting according. price, duration, ratings when i set

var type = 'price';
var sortAs = -1;

if i set

var type = 'price';
var sortAs = 1;

if i set

var type = 'duration';
var sortAs = -1;

or

var type = 'duration';
var sortAs = 1;

All condition gives me same result. i thing type is not accepting. what am i doing wrong.

Upvotes: 0

Views: 53

Answers (3)

Farhan Tahir
Farhan Tahir

Reputation: 2134

What you are trying to do is

{ type: sortAs } 

where type is being used as object key, what you should do is

{ [type]: sortAs }  

Here type is being used as variable to create dynamic key for the object. thus this will create.

{duration: "-1"}

Following code should work for you.

function getAllExperiences(){
return new Promise(function(resolve,reject){
    var perPage = 10
    var page = req.params.page || 1
    var type = req.body.type;
    var sortAs = req.body.sortAs;
    Experiences
        .find({status:'live'})
        .sort({ [type] : sortAs })//, using es6 for generating dynamic property
        .skip((perPage * page) - perPage)
        .limit(perPage)
        .exec(function(err, results) {
            Experiences.count().exec(function(err, count) {
                if (err)
                {
                    reject(err);
                }else{
                    var obj = 
                    {
                        experiences: results,
                        current: page,
                        pages: Math.ceil(count / perPage)
                    };
                    resolve(obj);
                }
            })
        })
});
}

Upvotes: 2

Artur P.
Artur P.

Reputation: 896

function getAllExperiences(){
    return new Promise(function(resolve,reject){
        var perPage = 10
        var page = req.params.page || 1
        var type = req.body.type;
        var sortAs = Number(req.body.sortAs);
        var sort = {};
        sort[type] = sortAs;
        Experiences
            .find({status:'live'})
            .sort(sort)//,{duration:durationValue}
            .skip((perPage * page) - perPage)
            .limit(perPage)
            .exec(function(err, results) {
                Experiences.count().exec(function(err, count) {
                    if (err)
                    {
                        reject(err);
                    }else{
                        var obj = 
                        {
                            experiences: results,
                            current: page,
                            pages: Math.ceil(count / perPage)
                        };
                        resolve(obj);
                    }
                })
            })
    });
}

Upvotes: 2

mickl
mickl

Reputation: 49945

You should change .sort({type:sortAs})//,{duration:durationValue} to

var sortDefinition = {};
sortDefinition[type] = sortAs;

//then in pipeline
.sort(sortDefinition)

Because in your example it will always try to sort by field with name typesince it's a key name in that code.

Upvotes: 1

Related Questions