Sign In Register

How can we help you today?

Start a new topic

How can I send a parameter and seek for it in my collection?

Hi,

 

We have a little doubt. In our game we have a quiz and each question in this quiz are in a GS runtime collection, like this:

 

[{ 
"_id" : { "$oid" : "58094bbc5e5528a4fffd7521"} , 
"question" : { 
 "id" : "1" ,
 "level" : "Default" ,
 "content" : "Default" ,
 "prompt" : "Question..." ,
 "answer" : [ 
  { "_correct" : "false" , "__text" : "Option 1"} ,
  { "_correct" : "false" , "__text" : "Option 2"} ,
  { "_correct" : "true" , "__text" : "Option 3"}
 ]
}
}]

 And in our C# code we have:

 

GSRequestData quiz_ = new GSRequestData ();
  quiz_.Add ("id", id);
  new GameSparks.Api.Requests.LogEventRequest ().SetEventKey ("loadQuiz")
   .SetEventAttribute("JSONData", quiz_)
   .Send ((response) => {
   if (!response.HasErrors) {
    Debug.Log ("OK");
    GSData data = response.ScriptData.GetGSData ("quiz_data");
    print ("Quiz data: " + data);
   } else {
    Debug.Log ("Error");
   }
  });

 In our Cloud Code we have:

 

var quizData = Spark.runtimeCollection("quiz");
var JSONData = {"question": {"id":Spark.data.JSONData}};

var currentQuiz = quizData.findOne(JSONData);
Spark.setScriptData("quiz_data", currentQuiz);

 I know how to get all the data at once, but I want to get it one by one with the "id", for example. How can I send a parameter or parameters (id, level and content) and seek for this question in my file?


Hi Carlos, 

I believe i've spotted your issue. " quizData = Spark.runtimeCollection("quiz");"

You are returning an entire collection here which is problematic now because you want to cull unwanted data, and will be problematic in the future as it will timeout your request / exceed our request size limits when this collection grows. 


You may want to avail of the Mongo Function findOne . You can see an example here where I query a Runtime Collection of player owned "fighters" by two fields. playerId and a number identifier for the particular object owned by the player: 

var fighter = collection.findOne({ $and : [{"playerId" : playerId}, {"fighterId" : fighterId}]}); 


Here is more documentation on Mongo Queries: https://docs.mongodb.com/v3.2/reference/method/db.collection.findOne/


Let me know if I can do anything else to help. 

Best Regards, Patrick. 



Hi Patrick,


Responding the questions... when I use the code:

new GameSparks.Api.Requests.LogEventRequest ().SetEventKey ("loadQuiz")
			.Send ((response) => {
			if (!response.HasErrors) {
				GSData data = response.ScriptData.GetGSData ("quiz_data");
                                Debug.Log ("OK");
				print ("Quiz data: " + data);
			} else {
				Debug.Log ("Error");
			}
		});

  with the Cloud Code:

var quizData = Spark.runtimeCollection("quiz");
Spark.setScriptData("quiz_data", quizData);

 I can get the data, but in this way I get all the data in the collection.


If I use "GSRequestData quiz_ = new GSRequestData ()" to send a id or any other parameter I get an error. So, I want to send those parameters, seek in my collection e return the question and the options of answer.


Thanks!

Hi Carlos, 

Just to confirm we are on the same page, you are able to successfully reference the data "SetEventAttribute("JSONData", quiz_)" in cloud code yes? 


You then wish to query your "quiz" collection based on the "id" field passed in your JSONData attribute and this is also working successfully? 


and finally you only wish to return or reference certain fields from "cuurentQuiz" which is assigned the value of the document returned from your query? 


Just wish to make sure I know exactly what you need assistance with :) 


Best Regards, Patrick.  

Login to post a comment