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