Sign In Register

How can we help you today?

Start a new topic

Spark.metaCollection("collectioname").find(); problems

Hi, I'm new to GameSparks and I encountered a problem I can't seem to figure out


 Spark.save("characterCollection", {
    "_id" : 0,
    "name" : "spec1",
    "hitPoints" : 5,
    "movement" : 3,
    "strength" : 3,
    "defense" : 3,
    "range" : 1,
    
    "sprite" : "spec1",
    "avatar" : "spec1"    
});
Spark.save("characterCollection", {
    "_id" : 1,
    "name" : "spec2",
    "hitPoints" : 5,
    "movement" : 3,
    "strength" : 3,
    "defense" : 3,
    "range" : 1,
    
    "sprite" : "spec2",
    "avatar" : "spec2"    

});


var cursor = Spark.metaCollection("characterCollection").find({"name" : "spec1"});
var current = cursor.curr();


going through test harness, current will turn into null. I don't know if my collection setup is wrong or my query is wrong.


All I want to do is make a database of characters then retrieve all relevant data via a query - in this case by its name.


Thanks in advance


 

I was told by someone on a different forum to try it this way but still didn't work...current is still null in the end


 Spark.runtimeCollection("characterCollection").insert([
    {
     "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"
    }
]);


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


i still want to fetch the document with name spec1 but even just empty query doesn't seem to work at the moment...

Hi Deo,


What exactly are you trying to do here ? I can see from the example given above that the data was inserted into the game. The following will return the data for you.

 

var cursor = Spark.runtimeCollection("characterCollection").find({"name":"spec1"});
Spark.setScriptData("data", cursor)

 


The problem in your original post was that you were inserting into runtime collection but trying to find the data in a meta collection. Our documentation contains more reading on this topic. You can find some relevant guide here.


Regards,

Liam

 Well what I am trying to do is save a reference table for characters into a metaCollection (if I understand it correctly it should be meta).


Then I am trying to figure out how to use said data for now I am just trying to output into debug to see if I can get the referencing correctly.


So based on what you've said, I tampered with it some more and ended with this:

1. I created a metaCollection in nosql explorer

2. Inserted docs for sample use

3. run this in a test event:

 

 

var name = Spark.getData().name;
if(name !== ""){
   Spark.setScriptData("data", Spark.metaCollection("characterCollection").find({ "name" : name }));
}else{
   Spark.setScriptData("data", Spark.metaCollection("characterCollection").find());
}

 

 

4.lastly I want to reference parts of it so I can use it. but I don't get that part, when I do something like below:

var test = Spark.getScriptData("data"); // test shows as type : ?
Spark.getLog().debug(test); //gives me an error

 i tried different iterations trying to access the data but no success.


Thank you for helping.

To be clearer I was expecting to refer to the data along the lines of:

Spark.getScriptData("data")[0].movement;

or

Spark.getScriptData("data").movement;

or

something along those lines


then it will give me the movement of the queried character

OK I think this is a bug?

var name = Spark.getData().name;

var test =  Spark.metaCollection('characterCollection').find({ "name" : name }); // works fine


Spark.getLog().debug(test.curr()); // returns null

Spark.getLog().debug(test.size()); // returns 1 as expected

Spark.getLog().debug(test.next()); // returns the JSON i want as expected

Spark.getLog().debug(test.next().movement); // returns the property I want.as expected


Conclusion:

SparkMongoCursor.curr() is broken ? returns null instead of the JSON similar to next without moving the cursor...

Hi Deo,


This is not a bug, it is correct behaviour.

When you create a cursor using find() the position of the cursor is initially set BEFORE the first element in the results.

A typical pattern for iterating the results would be:


while (cursor.hasNext()) {

    var doc = cursor.next();

    // Do something with the doc

}


The point here is that the first document is retrieved using the first call to cursor.next() which moves the cursor to the next (in this case, the first) document and then returns it.

So initially, there is no current document, hence the behaviour you are seeing.

The Mongo documentation makes this clear, defining the next() method as "Returns the next document in the cursor". Note this is NOT the same as returning the current document then moving on to the next one.


If the cursor did initially point at the first document, you wouldn't be able to correctly iterate the results with the above code, as you would miss the first document.


Kind regards,


Jon.

Login to post a comment