Search code examples
mongodbrestpostmanisodate

How do I insert an ISODate into mongoDB via Postman?


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.


Solution

  • 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}}}, .., .. }