74

I have a mongo database set up. creating a new date object in mongoDb create a date object in ISO format eg: ISODate("2012-07-14T00:00:00Z")

I am using node.js to connect to mongo database and query the database. when ever I create a new date object (new Date()) in javascript its creates a javascript date object eg: Wed Mar 06 2013 14:49:51 GMT-0600 (CST)

Is there a way to create an ISO date object in javascript so that I can send the object directly to the mongoDb and perform date query

I am able to perform the below query in mongoDb

db.schedule_collection.find({
  start_date: { '$gte': new Date(2012, 01, 03, 8, 30) }
})

but cannot perform when I send in an javascript date object from node

The mongodb cookbook provides an python example to query the mongo database using datetime module, but does not provide any example use javascript.

Any help is appreciated. Thanking you in advance

loganfsmyth
  • 156,129
  • 30
  • 331
  • 251
Joel James
  • 3,870
  • 9
  • 32
  • 47
  • 1
    If you're using nodejs you can use the `toISOString()` method defined in ECMAScript5. – Rahman Kalfane Mar 06 '13 at 21:02
  • 6
    JavaScript `Date` objects are what you use with MongoDB. There isn't a separate `ISODate` type you need to use. Can you give a specific example of something that isn't working? – JohnnyHK Mar 06 '13 at 21:49

5 Answers5

112

Try using the ISO string

var isodate = new Date().toISOString()

See also: method definition at MDN.

jbmusso
  • 3,436
  • 1
  • 25
  • 36
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
  • 12
    Mongodb driver only works with the date object. I must admit that the question is a bit unclear. The following answer solved my problem: http://stackoverflow.com/questions/21286599/inserting-and-querying-date-with-mongodb-and-nodejs/21286896#21286896 – h-kippo Feb 27 '14 at 13:54
  • 2
    a string is not an object so I'm guessing the object we want is just the new Date('...') object. So basically ISODate(..) is the same as new Date(..)? – Rivenfall Sep 24 '15 at 16:55
  • 1
    I found that using this method, the date is a date string instead of ISODate object. What I mean is that: [var isodate = new Date().toISOString()] => ["time" : "2018-03-08T08:15:15.919Z"] While [var isodate = new Date(new Date().toISOString())] => ["time" : ISODate("2018-03-08T08:15:16.097Z") ] – Ng Sek Long Mar 08 '18 at 08:28
  • This casted the input as string. Now I can't perform aggregate with this field. – Mark Odey Nov 26 '19 at 16:41
19

try below:

var temp_datetime_obj = new Date();

collection.find({
    start_date:{
        $gte: new Date(temp_datetime_obj.toISOString())
    }
}).toArray(function(err, items) { 
    /* you can console.log here */ 
});
krillgar
  • 12,596
  • 6
  • 50
  • 86
Abdullah Shahin
  • 1,002
  • 15
  • 19
14

In node, the Mongo driver will give you an ISO string, not the object. (ex: Mon Nov 24 2014 01:30:34 GMT-0800 (PST)) So, simply convert it to a js Date by: new Date(ISOString);

iheartweb
  • 141
  • 1
  • 2
5

I solved this problem instantiating a new Date object in node.js:...

In Javascript, send the Date().toISOString() to nodejs:...

var start_date = new Date(2012, 01, 03, 8, 30);

$.ajax({
    type: 'POST',
    data: { start_date: start_date.toISOString() },
    url: '/queryScheduleCollection',
    dataType: 'JSON'
}).done(function( response ) { ... });

Then use the ISOString to create a new Date object in nodejs:..

exports.queryScheduleCollection = function(db){
    return function(req, res){

        var start_date = new Date(req.body.start_date);

        db.collection('schedule_collection').find(
            { start_date: { $gte: start_date } }
        ).toArray( function (err,d){
            ...
            res.json(d)
        })
    }
};

Note: I'm using Express and Mongoskin.

fgborja
  • 269
  • 3
  • 3
5

This worked for me:

var start = new Date("2020-10-15T00:00:00.000+0000");
 //or
start = new date("2020-10-15T00:00:00.000Z");

collection.find({
    start_date:{
        $gte: start
    }
})...etc
new Date(2020,9,15,0,0,0,0) may lead to wrong date: i mean non ISO format (remember javascript count months from 0 to 11 so it's 9 for october)
Adel Ben Hamadi
  • 720
  • 10
  • 13