Sign In Register

How can we help you today?

Start a new topic
Answered

Getting data from MongoDB in Cloud Code.

Hello.

I have some custom data collection for my card game in MongoDB (it's part of the game's logic). The collection's name is "cards_hand".


I would like to get record of given name from that collection with my C++ client. I know I can ask MongoDB for data in my Cloud Code. But how exactly?


Best Answer
Hey Peter,

So, is it that you want to get the "name" element for each item in the collection, or that you want to search by the name and return the document(s) with that name, and all its details?

I expect its the latter, so i'll show you what ive got for a solution to that...

[1] - You will need to setup a LogEventRequest which takes one attribute, a string, the name of the element in the doc you want to search. I called mine 'name'
[2] - In your cloud code, you will search the collection 'card_hand' and use the 'findOne' method on your collection.
[3] - You then want to send the document you've found back as script-data.


 

// IF YOU KNOW THERE WILL ONLY BE ONE ELEMENT RETURNED, USE findOne //
var searchInput = Spark.getData().name;
var searchResult = Spark.runtimeCollection("cards_hand").findOne({ "" : searchInput });
Spark.setScriptData("search_Result", searchResult);

// IF YOU KNOW THERE WILL MORE THAN ONE ELEMENT RETURNED, USE find //
var searchInput = Spark.getData().name;
var searchResults = Spark.runtimeCollection("cards_hand").find({ "" : searchInput });
Spark.setScriptData("search_Result", searchResults);

 


[4] - Now you can test this in the Test Harness, and your response should be a document, in JSON format will all the details for the doc with that name...


That should get you started.
When you call the log event request from the client, you will get the blue text in the response script-data.

Take a shout at getting the data back yourself and get back to us if you are still having problems.

Hope that helps,
-Sean


Hey Peter,

Do you need help on the client-side of things or just in the cloud code getting specifics back? Can you give me an example of what the JSON structure of the document in your "cards_hand" might look like so i can give you an example that should work for you?

Thanks,
Sean

 

I am not 100% sure about client-side so I would love to see it too, if it's not a problem.


My collection contains of entries like:

{ "_id": { "$oid": "5658f896e4b0f1c72f4fe066" }, "#": 1, "playerID": 43, "area": "castle1", "name": "Farm", "description": "", "type": "building", "cost": null, "attack": 0, "defense": 0, "hp": 5 }

{ "_id": { "$oid": "ar38f896e4vdf1c72f4fe064" }, "#": 4, "playerID": 43, "area": "castle2", "name": "Goblin", "description": "Goblin warrior", "type": "creature", "cost": {"red": 1, "black": 1"}, "attack": 5, "defense": 3, "hp": 1, "special skill": "climbing the walls" }

Im not 100% about the c++ sdk myself :P
Let me  sort something out in cloud code, and get back to you in a bit.
Do you know how to call a LogEventRequest client-side?

-Sean

 

Answer
Hey Peter,

So, is it that you want to get the "name" element for each item in the collection, or that you want to search by the name and return the document(s) with that name, and all its details?

I expect its the latter, so i'll show you what ive got for a solution to that...

[1] - You will need to setup a LogEventRequest which takes one attribute, a string, the name of the element in the doc you want to search. I called mine 'name'
[2] - In your cloud code, you will search the collection 'card_hand' and use the 'findOne' method on your collection.
[3] - You then want to send the document you've found back as script-data.


 

// IF YOU KNOW THERE WILL ONLY BE ONE ELEMENT RETURNED, USE findOne //
var searchInput = Spark.getData().name;
var searchResult = Spark.runtimeCollection("cards_hand").findOne({ "" : searchInput });
Spark.setScriptData("search_Result", searchResult);

// IF YOU KNOW THERE WILL MORE THAN ONE ELEMENT RETURNED, USE find //
var searchInput = Spark.getData().name;
var searchResults = Spark.runtimeCollection("cards_hand").find({ "" : searchInput });
Spark.setScriptData("search_Result", searchResults);

 


[4] - Now you can test this in the Test Harness, and your response should be a document, in JSON format will all the details for the doc with that name...


That should get you started.
When you call the log event request from the client, you will get the blue text in the response script-data.

Take a shout at getting the data back yourself and get back to us if you are still having problems.

Hope that helps,
-Sean

Thank you! That's complex and clear answer :)

Login to post a comment