Sign In Register

How can we help you today?

Start a new topic
Solved

runtimeCollection save() and dates

I'm have a runtimeCollection that has a date field stored as JS Date objects




so code like this: 

var object = {
    "textField": "test",
    "dateField": new Date()
};
Spark.runtimeCollection("collection").insert(object);

Will generate an object in mongo that we can query with nice date operators, so in NoSql Explorer we can do a query like:

{"dateField":{"$lte":{"$date":"2016-08-28T15:40:42.235Z"}}} on our collection and get our row back out.


now we run this code: 

var object = Spark.runtimeCollection("collection").findOne();
Spark.runtimeCollection("collection").save(object);

We expect this to make no significant change to our data, but in this case, when we repeat our query:

{"dateField":{"$lte":{"$date":"2016-08-28T15:40:42.235Z"}}} We no longer get our row back.





Hey Arnold,

This is a strange one, but i figured out what was happening.
The insert() and save() functions are working fine but the findOne() function is actually stripping out the $date tag and you are left with just the number. Therefore your query doesn't recognize the date anymore.

Below is the noSQL data before calling the findOne() method ( so, just running the first piece of cloud-code you have there )

Before FindOne()


 And this is the noSQL after running the save() method...

This is probably a bug in our system, so until it is fixed you can try adding that date-tag back by creating a new date from the number and adding that back into you collection...

 

var object = Spark.runtimeCollection("collection").findOne();
var UTC = object["dateField"];
var newDate = new Date(UTC);
object["dateField"] = newDate;
Spark.runtimeCollection("collection").save(object);

 Hope that helps,
-Sean


I'm seeing the same issue with the find() function, is there anyway to work with dates in gamesparks? Do I have to change everything to timestamps?

Well you can get the date back out in your cloud code using new Date(timestamp), this will create a javascript date object from it which you can use to compare dates like if(timeNow < timePassed)

Does this help?
-Sean

 

unfortunately, no, I was really hoping to be able to use the native date object in mongoDB, without having to add alot of code. I have re-factored my code to simply deal in javascript timestamps.

We are going to address a fix for this in the next release. I'll keep you updated.
-Sean

 

Hey Arnold,

This problem has been fixed in the latest release.

Sean

 

The problem is still there, just run this piece of cloud code

  

var coll = Spark.runtimeCollection("Test");
var doc = {
    "code" : "ALPHA",
    "startDate" : new Date (),
}

var time = doc.startDate.getTime();

coll.insert(doc);

time = doc.startDate.getTime();

doc = coll.findOne({"code" : "ALPHA"});

time = doc.startDate.getTime();

  the last line will fail because startDate is not a Date object

Thanks for that Christian, the team is investigating this.


Oisin

I just want to let you know that this problem is not critical on our project. We've decided to keep our date fields as number (in ms). It's probably more efficient that way anyway , no Date objects to create.

Login to post a comment