Sign In Register

How can we help you today?

Start a new topic
Answered

List all players, list all teams from Cloud Code

Hi, we are trying to build a game which you have to play in teams.

Is there an easy way to list all players and all teams in Cloud Code?


Best Answer

Hi Jose,


To accomplish listing players I would recommend that you mirror your 'players' collection (see this Tips and Tricks post for a guide on how to implement this). You can then query your mirrored collection using 'Spark.runtimeCollection(<mirroredplayercollection>).find()'

You can do the same thing with your teams by using cloud code in CreateTeamResponse and DropTeamResponse (and if you need to track the number of players in a team - JoinTeamResponse and LeaveTeamResponse) to ensure that your mirrored collection is kept in sync.

 

I'd also suggest that you limit or filter your results rather than getting the entire collection as this could become quite expensive as your collections grow. For example, you could retrieve, say, 50 entries as follows 'Spark.runtimeCollection("myCollection").find().limit(50);' and display them on a results page, then if the user selects next page/more results, perform another query of 50 with 'Spark.runtimeCollection("myCollection).find().skip(50).limit(50);', and so on.


If you run into any problems implementing this, or have any more questions, please let us know.


Regards,

Vinnie

1 Comment

Answer

Hi Jose,


To accomplish listing players I would recommend that you mirror your 'players' collection (see this Tips and Tricks post for a guide on how to implement this). You can then query your mirrored collection using 'Spark.runtimeCollection(<mirroredplayercollection>).find()'

You can do the same thing with your teams by using cloud code in CreateTeamResponse and DropTeamResponse (and if you need to track the number of players in a team - JoinTeamResponse and LeaveTeamResponse) to ensure that your mirrored collection is kept in sync.

 

I'd also suggest that you limit or filter your results rather than getting the entire collection as this could become quite expensive as your collections grow. For example, you could retrieve, say, 50 entries as follows 'Spark.runtimeCollection("myCollection").find().limit(50);' and display them on a results page, then if the user selects next page/more results, perform another query of 50 with 'Spark.runtimeCollection("myCollection).find().skip(50).limit(50);', and so on.


If you run into any problems implementing this, or have any more questions, please let us know.


Regards,

Vinnie

Login to post a comment