How to show data in ascending order mongoosejs + expressjs

0

Hello everyone, could you help me? I am trying to show my database records in ascending order, but this is an object inside another and I want to organize by cashOut follows the photo of my DB.

I'm using the mongoose and expressjs below as I'm looking for the data:

    var Serial = function(){
    this.sumTotalBets = function(game){
        var totalBet = 0;
        game.findOne({gameStatus:'in progress'}, function(err, games){
            if(err){throw err}
            for(var i = 0; i < games.players.length; i++){
                totalBet += Number(games.players[i].bet);
            }
            totalBet = totalBet.toFixed(8);
            console.log('Soma de tudo ' + totalBet);

            for(i = 0; i < games.players.length; i++){
                var verify = Number(games.players[i].profit);
                if(totalBet > verify.toFixed(8)){
                    totalBet -= verify;
                    console.log('Continua ' + i + ' ' + totalBet.toFixed(8));
                }else{
                    console.log('Parou em ' + i + ' ' + totalBet.toFixed(8) + ' PayOut ' + (games.players[i].cashOut - 0.01).toFixed(2));
                }

            }
        });
    }
}
module.exports = new Serial();

RESULTS IN THE CONSOLE

Soma de tudo 0.00263300
Continua 0 0.00262765
Continua 1 0.00252380
Parou em 2 0.00252380 PayOut 100.98
Continua 3 0.00252180
Parou em 4 0.00252180 PayOut 1.99'

In short, I want to list players by cashOut in increasing order

    
asked by anonymous 12.05.2017 / 18:00

1 answer

0

If an object is passed, the allowed values are   are asc, desc, ascending, descending, 1, and -1

See the official documentation :

// mostre field" ascendente e "test" descendente
query.sort({ field: 'asc', test: -1 });

For example:

{'cashOut', 'asc'}

Note: When you do not specify sort (1, -1, etc.), it works in ascending order.

    
06.06.2017 / 15:05