GameSparks JSON MongoDB syntax issues? I am not getting back anything from my query
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
Not familiar, but typically calls tell about failures, does "Spark.save" return anything useful?
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.
Hi Mercutio604
{"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).