2

How do I insert an ISODate into MongoDB via Postman? I have looked around but examples/queries on this subject tend to be just for ways of getting various string formats.

I have an in-house API set up on my localhost so I am querying the database (MongoDB) with Postman. Queries & entries are written in JSON so I would do this like so usually:

   { "adminModifiedId": 1, "dateCreated" : { "$date": "1557510188"}, .., .. }

or

   { "adminModifiedId": 1, "dateCreated" : new Date(), .., .. }

Of course dates within MongoDB are in this format: ISODate("2019-01-21T17:41:27.107Z") but I just can't find the right solution here. I know that Postman does allow to set global & environmental variables within the Pre-request Script section but it does seem strange that a platform so established would not have a way to format or convert into an ISODate type.


Edited in response to @Danny_Dainton

Postman body as JSON request Postman body as JSON

Pre-request Script enter image description here

Erorr response enter image description here


I'll leave this for a few days to see if anyone can suggest a pre-established answer (that doesn't require a pre-request script). Otherwise I will mark mine correct as the only answer that has worked for me so far.

2 Answers2

1

You could use either of these methods in a Pre-request Script.

Using the moment lib, like this:

var moment = require('moment')
pm.globals.set("ISO_Date", moment())

More info about that here: https://stackoverflow.com/a/47823708/6028443

Or

Just use basic JS code like this to create the same timestamp:

pm.globals.set("ISO_Date", (new Date()).toISOString())

Once the variable is created, add {{ISO_Date}} reference to your request body.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • @Danny_Dainton Thanks. Neither of these are working for me unfortunately. My `body request` is `JSON` attempting to insert into `MongoDB`. So after adding the `Pre-request Script` this is: `{ "adminModifiedId": 1, "dateCreated" : {{ISO_Date}}, .., .. }`. With the error `[Invalid Json: Unexpected character ('-' (code 45)): was expecting comma to separate Object entries` – jesus g_force Harris May 11 '19 at 00:19
  • Have you tried wrapping that in quotes `"{{ISO_Date}}"` - This is the syntax in Postman so there must be something else that you're doing. Can you update the question with an image of this in place and the error? – Danny Dainton May 11 '19 at 05:38
  • @Danny_Dainton I've just edited the original question with some screenshots to show what I've tried. `"{{ISO_Date}}"` did insert into the database but just as a string, i.e. `"dateCreated" : "2019-05-12T16:26:23.205Z"` rather than an `ISODate("2019-05-12T16:26:23.205Z")` – jesus g_force Harris May 12 '19 at 16:56
1

For whatever reason other solutions didn't work for me but may for others. This one resolved my issue though so may be of use.

Pre-request Script

    let t = Date.now()
    pm.environment.set('t', t);

Body (sample)

    { "adminModifiedId": 1, "dateCreated" : { "$date": {{t}}}, .., .. }