Sign In Register

How can we help you today?

Start a new topic
Answered

Basic Question: Read value from Cursor Object

 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

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

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"];
}

 

Hope this helps!


-Pádraig 



1 person likes this
Login to post a comment