Querying for a user relative leaderboard with MongoDB -


i have collection of users, each user has points attribute (see schema below).

my end goal return sorted array of limited documents according specific user location, or in other words, small part of leaderboard relative specific user location.

currently query , sort users, find user want in returned array , slice array according that. wondering if there query save me returning users.

sample user schema:

/* 1 */ {     "_id" : objectid("..."),     "points" : 5852 ,     "key" : "user1" }  /* 2 */ {     "_id" : objectid("..."),     "points" : 3835,     "key" : "user2" }  /* 3 */ {     "_id" : objectid("..."),     "points" : 1984,     "key" : "user3" }  /* 4 */ {     "_id" : objectid("...."),     "points" : 2437,     "key" : "user4" } 

lets assume want query return 3 documents sorted points - document of "user4" (my relative user), document after him in leaderboard ("user3" in example above) , 1 document before him ("user2" in above example)

thanks!

edit: im using mongoose if simplifies things.

edit2: please see below expected output -

 /* 1 */     {         "_id" : objectid("..."),         "points" : 3835,         "key" : "user2"     } /* 2 */     {         "_id" : objectid("...."),         "points" : 2437,         "key" : "user4"     }  /* 3 */     {         "_id" : objectid("..."),         "points" : 1984,         "key" : "user3"     } 

note "user1" not appear in output query asks 1 user ranked above , 1 user ranked below "user4"

sounds you're trying this: rank leaderboard in mongo surrounding players

as says in answer question, easiest way 3 queries. though that's increase in number of queries, data transfer reduced.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -