node.js ISODate 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8848314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ISODate is not defined
提问by coure2011
I am trying to get results from mongodb using nodejs/mongoose.
我正在尝试使用 nodejs/mongoose 从 mongodb 获取结果。
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
错误: ISODate is not defined
回答by qiao
Note that ISODateis a part of MongoDB and is not available in your case. You should be using Dateinstead and the MongoDB drivers(e.g. the Mongoose ORM that you are currently using) will take care of the type conversion between Dateand ISODatebehind the scene.
请注意,这ISODate是 MongoDB 的一部分,在您的情况下不可用。您应该使用Date,而不是和MongoDB的驱动器(如猫鼬ORM,你正在使用)将采取之间照顾类型转换Date和ISODate幕后。
回答by estellezg
In my case, I was converting a query with ISODates
就我而言,我正在使用 ISODates 转换查询
let dateString = "2014-01-22T14:56:59.301Z";
$gte : ISODate(dateStr)
in node.js is
在 node.js 中是
$gte : new Date(dateStr)
回答by Jitendra
You can simply use as follow to convert dates in ISO string :
您可以简单地使用如下转换 ISO 字符串中的日期:
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);
});
回答by Rahul Bhat
Instead of ISO use "new Date" node js will take care of ISO itself, no need to write ISO just simply use "new Date"
代替 ISO 使用“新日期”节点 js 将处理 ISO 本身,无需编写 ISO 只需简单地使用“新日期”
回答by Farrukh Malik
Convert date to MongoDB ISODate format in JavaScript using Moment JS
使用 Moment JS 在 JavaScript 中将日期转换为 MongoDB ISODate 格式
MongoDB uses ISODate as their primary date type. If you want to insert a date object into a MongoDB collection, you can use the Date()shell method.
MongoDB 使用 ISODate 作为其主要日期类型。如果要将日期对象插入到 MongoDB 集合中,可以使用Date()shell 方法。
You can specify a particular date by passing an ISO-8601 date string with a year within the inclusive range 0 through 9999 to the new Date()constructor or the ISODate() function. These functions accept the following formats:
您可以通过将包含 0 到 9999 包含范围内的年份的 ISO-8601 日期字符串传递给new Date()构造函数或 ISODate() 函数来指定特定日期。这些函数接受以下格式:
- new Date(
"<YYYY-mm-dd>")returns the ISODate with the specified date. - new Date(
"<YYYY-mm-ddTHH:MM:ss>")specifies the datetime in the client's local timezone and returns the ISODate with the specified datetime in UTC. - new Date(
"<YYYY-mm-ddTHH:MM:ssZ>")specifies the datetime in UTC and returns the ISODate with the specified datetime in UTC. - new Date() specifies the datetime as milliseconds since the Unix epoch (Jan 1, 1970), and returns the resulting ISODate instance.
- new Date(
"<YYYY-mm-dd>")返回具有指定日期的 ISODate。 - new Date(
"<YYYY-mm-ddTHH:MM:ss>")指定客户端本地时区中的日期时间,并返回具有指定日期时间的 ISODate UTC 格式。 - new Date(
"<YYYY-mm-ddTHH:MM:ssZ>")以UTC 指定日期时间并返回具有指定UTC 日期时间的ISODate。 - new Date() 将日期时间指定为自 Unix 纪元(1970 年 1 月 1 日)以来的毫秒数,并返回结果 ISODate 实例。
If you are writing code in JavaScript and if you want to pass a JavaScript date object and use it with MongoDB client, the first thing you do is convert JavaScript date to MongoDB date format (ISODate). Here's how you do it.
如果您使用 JavaScript 编写代码,并且想要传递 JavaScript 日期对象并将其与 MongoDB 客户端一起使用,那么您要做的第一件事就是将 JavaScript 日期转换为 MongoDB 日期格式 (ISODate)。这是你如何做到的。
var today = moment(new Date()).format('YYYY-MM-DD[T00:00:00.000Z]');
console.log("Next day -- " + (reqDate.getDate() + 1))
var d = new Date();
d.setDate(reqDate.getDate() + 1);
var tomorrow = moment(d).format('YYYY-MM-DD[T00:00:00.000Z]');
You can pass today and tomorrow object to MongoDB queries with new Date() shell method.
您可以使用 new Date() shell 方法将今天和明天的对象传递给 MongoDB 查询。
MongoClient.connect(con, function (err, db) {
if (err) throw err
db.collection('orders').find({ "order_id": store_id, "orderDate": {
"$gte": new Date(today), "$lt": new Date(tomorrow)}
}).toArray(function (err, result) {
console.log(result);
if (err) throw err
res.send(result);
})
})

