Sign In Register

How can we help you today?

Start a new topic

How to get random teams with custom data?

Hi,


I would like to know the best approach for this problem as it will show us the best practices how to solve other similar problems in future.


What I need is the ability for player to find 20 random teams(guilds). I know that probably best way how to do this is by creating a custom collection eg. "guild_list" which will contain all team ids. Then I can find random teams via cloud script. So far so good and I hope this is the best practice. 


What I want on top of this is to retrieve actual number of members for each of the 20 randomly chosen guilds, so the player will not have to query all 20 guilds with "GetTeamRequest" just to get actual member count.

I guess the best would be to store the current members count in the custom "guild_list" collection. The problem with this is how to keep the members count updated in the "guild_list" custom collection? The players can leave/join the guild in any time so the actual number of player will fluctuate rapidly.

I could use events and each time any member of the guild leaves/joins the team it will also updates the members count in "guild_list" collection. However I am afraid of concurrency collisions as 2 or more players could be accessing this entry in "guild_list" in a same time(when 2 players decide to leave/join guild in a same time), isn't is a problem?


Is there a better solution for this? Am I approaching this problem right? Because there might be many more situations like this in our game where we want to have dynamic data stored in custom collection for anybody to access as calling dozens of API calls just to get specific information about players/team could be very heavy on your service.


Thanks!

Jan


Hi Jan,


You're solution sounds reasonable - you can use the "$inc" operator to perform an atomic increment (or decrement) to a numeric value in a single document, which will solve your concurrency issue.

That is, don't try to set the new value as such, just $inc by 1 when someone joins, and $inc by -1 when someone leaves. That way, if 2 players are joining/leaving at the same time, Mongo will handle this atomically for you and the final value will remain correct.


For more information see the Mongo documentation: https://docs.mongodb.com/manual/reference/operator/update/inc/


Kind regards,


Jon.

Thanks for your fast reply! Didn't know about $inc operator... still learning. This sounds perfect for this purpose. Will try it, thanks a lot!

Just want to let everyone know that i have also solved this by using $lookup aggregation to "join" both collections together to return both information about members count together with extra info contained in custom collection "guildData". Result looks something like this:


var extraData = teamsSystemCollection.aggregate(

 

    {$sample:{size:20}}

    ,

    {

      $lookup:

        {

          from: "script.guildData",

          localField: "_id",

          foreignField: "guildID",

          as: "extraData"

        }  

   }

       ,

    {$project: {GuildMembersCount: { $size: "$members" },guildID:1,extraData:1}}

   )

Login to post a comment