简体   繁体   中英

How do I sort database data in mongodb javascript? ( node.js )

I was creating bot in Discord.js. It has level system ( which you gain by writing things in the chat ) and I wanted to make a leaderboard, but I have to somehow sort the database out to do that, for example this is how it looks like now ( this is an example )

userId:"347707015956791297"
level:3
xp:231

userId:"267229922693611520"
level:1
xp:36

userId:"124575187579568129"
level:1
xp:81

userId:"466944003674472482"
level:4
xp:468

How do I make it look like this ( and if the person has same level, then it sorts it through xp ):

userId:"347707015956791297"
level:4
xp:468

userId:"267229922693611520"
level:3
xp:231

userId:"124575187579568129"
level:1
xp:81


userId:"466944003674472482"
level:1
xp:36

要按两个字段排序,只需在find之后调用sort并指示必要的字段和升序/降序:

db.users.find({ }).sort( { level: -1, xp: -1 } )

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