# Inserting usable timestamps in Mongo

**URL:** <https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546>\
**Category:** Connecting to NoSQL\
**Created:** [November 13, 2014, 11:20pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546 "2014-11-13T23:20:05Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hijakk](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hijakk](https://community.dreamfactory.com/u/hijakk)\
**Post date:** [November 13, 2014, 11:20pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/1 "2014-11-13T23:20:05Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [November 14, 2014, 7:30pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/2 "2014-11-14T19:30:00Z")

</div>

Try this:

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

---

<div class="post-metadata">

**Author:** ![hijakk](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hijakk](https://community.dreamfactory.com/u/hijakk)\
**Post date:** [November 14, 2014, 8:02pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/3 "2014-11-14T20:02:07Z")

</div>

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](https://localhost:443/rest/test_app_mongo/appointment)

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [November 14, 2014, 8:23pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/4 "2014-11-14T20:23:21Z")

</div>

You are missing a closing paren on Date().

---

<div class="post-metadata">

**Author:** ![hijakk](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hijakk](https://community.dreamfactory.com/u/hijakk)\
**Post date:** [November 14, 2014, 8:37pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/5 "2014-11-14T20:37:37Z")

</div>

Darn - I am indeed.  
Just tried

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

```

and still go the same 500 json syntax error

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [November 14, 2014, 8:58pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/6 "2014-11-14T20:58:14Z")

</div>

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")
        }
    ]
}
```

---

<div class="post-metadata">

**Author:** ![hijakk](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hijakk](https://community.dreamfactory.com/u/hijakk)\
**Post date:** [November 19, 2014, 5:36pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/7 "2014-11-19T17:36:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [November 19, 2014, 6:33pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/8 "2014-11-19T18:33:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hijakk](https://avatars.discourse-cdn.com/v4/letter/h/59ef9b/32.png) [@hijakk](https://community.dreamfactory.com/u/hijakk)\
**Post date:** [November 19, 2014, 6:51pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/9 "2014-11-19T18:51:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![toddappleton](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/toddappleton/32/159_2.png) [@toddappleton](https://community.dreamfactory.com/u/toddappleton)\
**Post date:** [November 19, 2014, 7:02pm UTC](https://community.dreamfactory.com/t/inserting-usable-timestamps-in-mongo/546/10 "2014-11-19T19:02:21Z")

</div>

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()’.
