How to show data in ascending order mongoosejs + expressjs

Asked

Viewed 329 times

0

Can you help me? I am trying to show my Database records in ascending order, but it is an object within another and I want to organize by cashout follows the photo of my DB. Meu banco de dados

I am using the Mongoose and expressjs below as I am searching 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();

CONSOLE RESULT

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`

Summing up I want to list players by where increasing cashout

1 answer

0

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

See the employee documentation:

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

For example:

{'cashOut', 'asc'}

Note: When Voce does not specify Sort (1, -1, etc), it works in ascending order.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.