1

Hello actuallly i am tring to make a leaderboard command for my discord bot so wanted to make this

User Shema Example

{ _id: something, points: 100 }

I have every players in Array example

[
{ _id: something, points: 100 },
{ _id: something2, points: 300 },
{ _id: something3, points: 200 },
{ _id: something4, points: 50 }
]

I wanted to get points by ascending order by points then I want to get the name this is how it want to be like

something4 - 50
something - 100
something3 - 200
something - 300
Sudhan
  • 306
  • 4
  • 13

1 Answers1

2

Refer to this,

MongoClient.connect("link_here", function(err, db) {
  if (err) throw err;
  var dbo = db.db("mydatabase");
  var sortSelection = { points: 1 };

  dbo.collection("yourcollection").find().sort(sortSelection).toArray(function(err, result) {
    if (err) throw err;
    console.log(result);
    db.close();
  });
});
yanko
  • 164
  • 9