Inserting usable timestamps in Mongo


#1

I’m going to be inserting and query records via the dreamfactory rest interface into a mongoDB back end. How do I create a date object in my json object, such that I can insert
{“next_appointment”:“2014-11-15T17:30:00”} and have it parsed as a Mongo timestamp?


#2

Try this:

{“next_appointment”:new Date(“2014-11-15T17:30:00”)}


#3

I get back

{
  "error": [
    {
      "message": "JSON Error: Syntax error, malformed JSON",
      "code": 500
    }
  ]
}

When I post

{"next_appointment":new Date("2014-11-15T17:30:00"}

against my endpoint
https://localhost:443/rest/test_app_mongo/appointment


#4

You are missing a closing paren on Date().


#5

Darn - I am indeed.
Just tried

{"next_appointment":new Date("2014-11-15T17:30:00")}

and still go the same 500 json syntax error


#6

If you can provide the complete request and response data that would be helpful. The preferred way to create records is to wrap them with a record[] wrapper. Here is the data I posted to my MongoDB service and I got no errors. How are you making the HTTP call?

POST /rest/dfmongo/todo
{
    "record": [
        {
            "name": "test",
            "complete": false,
            "next_appointment": new Date("2014-11-15T17:30:00")
        }
    ]
}

#7

Apologies, I didn’t get an email update when the last comment happened
I was using the dreamfactory swagger API interface and attempting to do exactly

POST https://localhost:443/rest/test_app_mongo/appointment
{"next_appointment":new Date("2014-11-15T17:30:00")}

Which comes back with:

{
  "error": [
    {
      "message": "JSON Error: Syntax error, malformed JSON",
      "code": 500
    }
  ]
}

I hadn’t run into documentation on the record wrapper, so I think that’s what I was looking for. I’m still not exactly sure, though, as running through the swagger API interface I’m posting

POST https://localhost:443/rest/test_app_mongo/appointment
{"record":[{"name":"test", "next_appointment":new Date("2014-11-15T17:30:00")}]}

and I’m getting back a 500 of malformed JSON, still:

{
  "error": [
    {
      "message": "JSON Error: Syntax error, malformed JSON",
      "code": 500
    }
  ]
}

It’s looking like we’re probably going to just use device based milisecond encoded dates, but this is nagging at me a bit.


Inserting ISODate type objects into MongoDB
#8

The API Docs interface can’t call JavaScript functions so when you say

new Date(“2014-11-15T17:30:00”)

that does not work in the API Docs. If you write an app with JavaScript that calls the REST API it will work.


#9

Ah, I see - so there’s no way to insert Mongo dates using just the basic REST API?


#10

You can but the dates must be in the correct format. From the API docs you can POST

{“name”:“1212”,“complete”:false,“next_appointment”:“2014-11-15T17:30:00.000Z”}

This eliminates the need for ‘new Date()’.