Sign In Register

How can we help you today?

Start a new topic
Answered

MongoDB aggregate from Cloud Code

Hi!


I'm trying to get an aggregated score from MongoDB via Cloud Code with the following JS:

var http = Spark.getHttp("https://portal.gamesparks.net/rest/games/gameId/mongo/preview/levelScore-leaderboard/aggregate");
http.setBasicAuth("***", "***");
var match = { "$match": { level: { "$gte": Spark.getData().firstLevel, "$lte": Spark.getData().lastLevel } } };
var group = { "$group": { _id: "$userId", total: { "$sum": "$score" } } };
var pipelineArray = [match, group];
var jsonForm = { "pipeline": pipelineArray};
var response = http.postJson(jsonForm);
var responseJson = response.getResponseJson();

This almost works, but I get the following response:

"responseCode": -1,
"errorMessage": "Required String parameter 'pipeline' is not present"

Could someone help me out here?


Best Answer

ok, you can do this pretty easily.


If you create a new leaderboard that uses the same data as the partitioned one, you can select the score value and set the calculation to "sum". Leave the level out of this leaderboard.


This will create a leaderboard with one entry per player with the sum of the individual level scores for that player. This works as the event is set to be grouped.


If you are not clear on how to do this, please raise a ticket with your API key and we can take a closer look and help you through it.


Gabriel


And that was waaaay easier than I thought. Thank you so much for such helpful and swift responses! :)

Answer

ok, you can do this pretty easily.


If you create a new leaderboard that uses the same data as the partitioned one, you can select the score value and set the calculation to "sum". Leave the level out of this leaderboard.


This will create a leaderboard with one entry per player with the sum of the individual level scores for that player. This works as the event is set to be grouped.


If you are not clear on how to do this, please raise a ticket with your API key and we can take a closer look and help you through it.


Gabriel

Ok. So basically in the game we have different worlds that each consist of about 20 levels. Each level has it's unique high score. I want to calculate a sum of all the high scores in a world per player, and make a leaderboard out of that.

So you want to be able to calculate the sum of a players scores across multiple levels?


We don't generally give direct access to these leaderboard collections, as they are highly optimised for reads and ad hoc read like this might not perform so well.


You can setup another leaderboard for this that will pre-calculate this data for you.


If you can give us some details of what you are trying to do we can come up with the most appropriate solution.

I'm trying to calculate a sum of scores from a certain range of levels per user.

I've also tried doing it like this:

 

var collection = Spark.runtimeCollection("levelScore-leaderboard");
var match = { "$match": { level: { "$gte": Spark.getData().firstLevel, "$lte": Spark.getData().lastLevel } } };
var group = { "$group": { _id: "$userId", total: { "$sum": "$score" } } };
var json = collection.aggregate(match, group);

But this just returns an empty array.

Hei Petteri


Calling the portal rest endpoint from within cloud code is not something we really support. This endpoint is not for runtime operations, but for dev / administration purposes only.


Could you explain what you are trying to achieve?


Gabriel

Login to post a comment