[英]NodeJS MongoDB: Querying ISO Date
I have a collection of ISO dates stored in MongoDB as strings, like so:我有一个以字符串形式存储在 MongoDB 中的 ISO 日期集合,如下所示:
{ "date": "2014-12-12T03:33:33.333Z" },
{ "date": "2014-12-13T03:33:33.333Z" }
In the console, I can query these perfectly using在控制台中,我可以完美地使用这些查询
{ "date": ISODate("2014-12-12T03:44:00.000Z") }
However, I'm using the NodeJS driver, and I cannot use ISODate
.但是,我使用的是 NodeJS 驱动程序,我不能使用
ISODate
。 I have found several questions pertaining to this problem on here, but none of the proposed solutions seem to work.我在这里发现了几个与此问题有关的问题,但建议的解决方案似乎都不起作用。 For instance:
例如:
// These does not find any matches
db.find({ "date": new Date("2014-12-12T03:44:00.000Z") })
db.find({ "date": { '$eq': '2014-12-12T03:44:00.000Z' } })
db.find({ "date": { '$eq': new Date('2014-12-12T03:44:00.000Z') } })
//This throws an error stating $date is not an operator
db.find({ "date": { '$date': '2014-12-12T03:44:00.000Z' } })
Why are these queries failing?为什么这些查询失败?
Edit: Here's another sample, straight from the database:编辑:这是另一个示例,直接来自数据库:
{
"_id": "5a7e88f34b5916723589183f",
"date": "2014-12-12T03:42:00.000Z",
"granularity": 180
}
EDIT 2: This query produces the following error MongoError: $dateFromString requires that 'dateString' be a string, found: date with value 2014-12-12T03:44:00.000Z
编辑 2:此查询产生以下错误
MongoError: $dateFromString requires that 'dateString' be a string, found: date with value 2014-12-12T03:44:00.000Z
async loadCandle(date, granularity) {
date = date + ''; //Aded to ensure date is a string, but still get the error.
var candle = await this.data.collection('dates').findOne(
{ $expr :
{$eq :
[
{$dateFromString : {dateString : "$date"}},
new Date("2014-12-12T03:33:33.333Z") //Normally would pass in the `date` variable here
]
} });
because $date
is not an operator 因为
$date
不是运算符
you need to use $dateFromString
to convert string date to ISODate
for comparison 您需要使用
$dateFromString
将字符串日期转换为ISODate
进行比较
db.datez.find(
{$expr :
{$eq :
[
{$dateFromString : {dateString : "$date"}},
new Date("2014-12-12T03:33:33.333Z")
]
}
}
)
using aggregation 使用聚合
db.datez.aggregate([
{$match :
{$expr :
{$eq :
[
{$dateFromString : {dateString : "$date"}},
new Date("2014-12-12T03:33:33.333Z")
]
}
}
}
])
collection 采集
> db.datez.find()
{ "_id" : ObjectId("5a7e795e80aae386f73cf0fe"), "date" : "2014-12-12T03:33:33.333Z" }
{ "_id" : ObjectId("5a7e795e80aae386f73cf0ff"), "date" : "2014-12-13T03:33:33.333Z" }
>
result 结果
> db.datez.find({$expr : {$eq : [{$dateFromString : {dateString : "$date"}}, new Date("2014-12-12T03:33:33.333Z")]}})
{ "_id" : ObjectId("5a7e795e80aae386f73cf0fe"), "date" : "2014-12-12T03:33:33.333Z" }
You can use $dateToString operator which generate the string date of any specified format, which can be compared later.您可以使用 $dateToString 运算符生成任何指定格式的字符串日期,稍后可以进行比较。
For string comparison of date, input format should be YYYY-MM-DD, any other format would get fail for ranges date queries对于日期的字符串比较,输入格式应为 YYYY-MM-DD,对于范围日期查询,任何其他格式都会失败
Let me explain through example:让我通过例子来解释:
Here is my collection in mongoDb :这是我在 mongoDb 中的集合:
{
"_id" : ObjectId("5f2d0a0c632ec022e08c3191"),
"date" : ISODate("2020-07-12T00:00:00Z")
}
{
"_id" : ObjectId("5f2d0a12632ec022e08c3192"),
"date" : ISODate("2020-07-13T00:00:00Z")
}
Now the query to be fired from Node for comparison of such stored ISODates is as follow现在要从 Node 触发以比较此类存储的 ISODates 的查询如下
db.collection.aggregate(
[
{
$addFields: {
formattedDate: { // An extra field "formattedDate" is added in each document which can be compared later through pipeline using $match
$dateToString: {
format: "%Y-%m-%d",
date: "$date" // in "$date" date is variable from db
}
}
}
},
{
$match: {
formattedDate: {
$eq: "2020-07-12" // here you can provide your input date yyyy-mm-dd
}
}
}
]
)
So for above query you will get output as因此,对于上述查询,您将获得输出为
{
"_id" : ObjectId("5f2d0a0c632ec022e08c3191"),
"date" : ISODate("2020-07-12T00:00:00Z"),
"formattedDate" : "2020-07-12"
}
Hope this will help you or somebody else!希望这会帮助你或其他人!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.