سؤال

I am trying to get results from mongodb using nodejs/mongoose.

var dateStr = new Date(year,month,day,0,0,0);
var nextDate = new Date(year,month,day,23,59,59);

GPSData.find({"createdAt" : { $gte : new ISODate(dateStr), $lte:  new ISODate(nextDate) }}, function(err, data) {
  if(err)
    console.log(err); 
});

Error: ISODate is not defined

هل كانت مفيدة؟

المحلول

Note that ISODate is a part of MongoDB and is not available in your case. You should be using Date instead and the MongoDB drivers(e.g. the Mongoose ORM that you are currently using) will take care of the type conversion between Date and ISODate behind the scene.

نصائح أخرى

You can simply use as follow to convert dates in ISO string :

GPSData.find({"createdAt" : { $gte : new Date(year,month,day,0,0,0).toISOString(), $lte:  new Date(year,month,day,23,59,59).toISOString() }}, function(err, data) {
  if(err)
    console.log(err); 
});

In my case, I was converting a query with ISODates

let dateString = "2014-01-22T14:56:59.301Z";

$gte : ISODate(dateStr)

in node.js is

$gte : new Date(dateStr)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top