• Advertisement
Sign in to follow this  

GameSparks JSON MongoDB syntax issues? I am not getting back anything from my query

Recommended Posts

Anyone here familiar with GameSparks?
I am trying to make a collection (don't know if I am doing it right I am not well versed in JSON yet):

Spark.save("characterCollection", {
"characters" : [
{
"id" : 1,
"name" : "spec1",
"hitPoints" : 5,
"movement" : 3,
"strength" : 3,
"defense" : 3,
"range" : 1,

"sprite" : "spec1",
"avatar" : "spec1"
},
{
"id" : 2,
"name" : "spec2",
"hitPoints" : 5,
"movement" : 3,
"strength" : 3,
"defense" : 3,
"range" : 1,

"sprite" : "spec2",
"avatar" : "spec2"

}
]
});
Then I am trying to get the data from by searching via name "spec1", again I don't know if I am doing it right not familiar with that either >.<"

var characterList = Spark.metaCollection("characterCollection"); // this one works fine i think

var result = Spark.metaCollection('characterCollection').findOne({"characters" :{ "name" : "spec1"}});
Spark.getLog().debug(result); // result listed as null

Share this post


Link to post
Share on other sites
Advertisement

Spark.save returns a boolean for success/failure and the below code goes through success branch.

i changed the JSON formatting a bit to something i think is closer to right:

if(Spark.save("characterCollection", {
    "_id" : 0,
    "name" : "spec1",
    "hitPoints" : 5,
    "movement" : 3,
    "strength" : 3,
    "defense" : 3,
    "range" : 1,
    
    "sprite" : "spec1",
    "avatar" : "spec1"    
}))
{
     Spark.getLog().debug("added");  
}
else
{
     Spark.getLog().debug("not added");  
}

var cursor = Spark.runtimeCollection("characterCollection").find();
var current = cursor.curr();

in the debugger type thingy called test harness it shows "current" as null in this version.

Share this post


Link to post
Share on other sites

Hi Mercutio604

It looks like you're saving a single document with a single field ('characters'), which contains an array of objects. The query's failing because there is no matching document in the collection
 
If you change your Spark.save() stuff to:
 
Spark.runtimeCollection("characterCollection").insert([
{"id":1,
....
},
{"id":2,
....
},
...
]);

it should do what you want (i.e. create a separate document in the collection for each element of the array).
 
If not, or if you have any other questions about using GameSparks, please do not hesitate to get in touch with our support team via https://support.gamesparks.net/support/home we'd be happy to help in any way we can!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Advertisement