I'm struggeling with a really simple thing: I want to execute a DB Query and return a value. Please see my code, I'm performing a query and getting a SparkMongoCursor. From this cursor, I d like to read one field, which is "matchId".
I can't seem to find the syntax that allows me to do that. Can you help me?
function getRunningMatchOfCurrentPlayer(){
var playerId = Spark.getPlayer().getPlayerId();
//Look up Match with the current ID which are not finished
var query = {
$and: [ {
$or: [ { "TEAM_RED.PLAYER_ID" : playerId },
{ "TEAM_BLUE.PLAYER_ID" : playerId } ] },
{ "finished": "" }
] };
var matchInstance = Spark.runtimeCollection("playedMatches");
var match = matchInstance.find(query).findOne;
if(match.count() == 0){
Spark.setScriptError("error", "PLAYERHASNORUNNINGMATCH")
Spark.exit()
}
//HOW TO READ AN OBJECT FROM THE MATCH OBJECT??
}
Best Answer
C
Customer Support
said
almost 6 years ago
Hi Axel,
On runtimeCollections and metaCollections, find will return a SparkMongoCursor, while fineOne will return a single JSON document.
To iterate through the cursor and retrieve values, use the following code.
while(match.hasNext())
{
var value = match.next(); //value is now a json document
var matchId = value["matchId"];
}
Hope this helps!
-Pádraig
1 Comment
Customer Support
said
almost 6 years ago
Answer
Hi Axel,
On runtimeCollections and metaCollections, find will return a SparkMongoCursor, while fineOne will return a single JSON document.
To iterate through the cursor and retrieve values, use the following code.
while(match.hasNext())
{
var value = match.next(); //value is now a json document
var matchId = value["matchId"];
}
Axel Dabee
I'm struggeling with a really simple thing: I want to execute a DB Query and return a value. Please see my code, I'm performing a query and getting a SparkMongoCursor. From this cursor, I
d like to read one field, which is "matchId".
I can't seem to find the syntax that allows me to do that. Can you help me?
Hi Axel,
On runtimeCollections and metaCollections, find will return a SparkMongoCursor, while fineOne will return a single JSON document.
To iterate through the cursor and retrieve values, use the following code.
Hope this helps!
-Pádraig
Customer Support
Hi Axel,
On runtimeCollections and metaCollections, find will return a SparkMongoCursor, while fineOne will return a single JSON document.
To iterate through the cursor and retrieve values, use the following code.
Hope this helps!
-Pádraig
1 person likes this
-
Documentation Notes
-
Design issues with user events
-
Using NoSQL
-
Runtime Collections vs Metadata Collections
-
Anonymous authentication from browser app
-
Modules
-
Movement With Unity
-
Problem with url parameters for downloadables
-
Querying NoSql GameSparks database
-
Challenge accesType
See all 2487 topics