Sign In Register

How can we help you today?

Start a new topic
Answered

MongoDB query return value

From my code

var x = Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()});
Spark.setScriptData("testA",x);

Spark.runtimeCollection("R_Test").insert({"player_id":Spark.getPlayer().getPlayerId(),"uid":mUtility().generateUID()});

var y = Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()});
Spark.setScriptData("testB",y);

I expect testA to be an empty array and testB to contain my newly inserted query, yet, the response I receive is 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "M_Utility": "loaded",
  "testA": [
   {
    "_id": {
     "$oid": "55b62f9ae4b025d8e0e0f85f"
    },
    "player_id": "559f842a845fb88eba4d75e4",
    "uid": "9325c9db-3316-464b-aeaa-64b533223256"
   }
  ],
  "testB": [
   {
    "_id": {
     "$oid": "55b62f9ae4b025d8e0e0f85f"
    },
    "player_id": "559f842a845fb88eba4d75e4",
    "uid": "9325c9db-3316-464b-aeaa-64b533223256"
   }
  ]
 }
}

  Can someone from tech support clue me as to why it happens?


When I try to stringify it using 

var x = JSON.stringify(Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()}));
Spark.setScriptData("testA",x);

Spark.runtimeCollection("R_Test").insert({"player_id":Spark.getPlayer().getPlayerId(),"uid":mUtility().generateUID()});

var y = JSON.stringify(Spark.runtimeCollection("R_Test").find({"player_id":Spark.getPlayer().getPlayerId()}));
Spark.setScriptData("testB",y);

 I receive this response 

{
 "@class": ".LogEventResponse",
 "scriptData": {
  "M_Utility": "loaded",
  "testA": "{}",
  "testB": "{}"
 }
}

 


Best Answer

 Hey Davendra,


That is very confusing! But i figured it out after a while.

So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.

So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.


As for using JSON.stringify, you will need to tell that method how to serialize the object.


Hope that helps,
-Sean



Answer

 Hey Davendra,


That is very confusing! But i figured it out after a while.

So, the problem (i think) is that the collection.find() function returns a mongo-cursor. So, even when it should return null, it is return a position. That position would appear to be the same as for when one element is inserted. So when you return the script data is gets the cursor for that collection which by the end of the script contains one element. So it doesn't return null the first time.

So the solution is to use findOne, instead of find as this will return the actual document. Therefore, it will return null if it cant find anything.


As for using JSON.stringify, you will need to tell that method how to serialize the object.


Hope that helps,
-Sean


I do believe that you are absolutely right. Thanks Sean

you've been mongo'd!

 

Login to post a comment